如何在Java JUnit中结合这两个RunWith?

问题描述 投票:0回答:1

@ RunWith(SpringRunner.class)@Runwith(PowerMockRunner.class)

如何合并这两个注释。由于RunWith仅支持单个值。

java junit powermock
1个回答
0
投票

如果您正在使用JUnit 5,则您不能使用它,因为PowerMock还不是support it

使用JUnit 4使用@Runwith(PowerMockRunner.class),并使用SpringRunner.class注释添加@PowerMockRunnerDelegate。例如

    import org.springframework.test.context.junit4.SpringRunner; 
    import org.powermock.modules.junit4.PowerMockRunner;
    import org.powermock.modules.junit4.PowerMockRunnerDelegate;

    @RunWith(PowerMockRunner.class)
    @PowerMockRunnerDelegate(SpringRunner.class)
    @PrepareForTest(LogHelper.class)
    @WebMvcTest(controllers = UserController.class)
    public class UserControllerTest {

        @Autowired
        private MockMvc mockMvc;

        @Mock
        private UserService userService;

        @Test
        public void saveShouldReturnOK() {
            mockStatic(LogHelper.class);
            doNothing().when(LogHelper.info(anyString()));
            ....
        }
    } 
© www.soinside.com 2019 - 2024. All rights reserved.