This question already has answers here:
Multiple RunWith Statements in jUnit

(8个答案)


6个月前关闭。





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

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

最佳答案

如果您正在使用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()));
            ....
        }
    }

09-13 07:15