我正在为我的RestControllers
编写TestCases
对于每个ControllerTest类
,我使用以下注释
@WebAppConfiguration
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebConfig.class, TestAppConfig.class})
所以,我决定定义自己的注释,其中包含所有这些注释,如下所示。@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@WebAppConfiguration
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebConfig.class, TestAppConfig.class})
public @interface ControllerTest {
}
然后,我为所有我的ControllerTest类
只使用了一个注解。
@ControllerTest
public class XXControllerTest {
}
进行这次修改后,测试未通过
java.lang.IllegalArgumentException: WebApplicationContext is required
at org.springframework.util.Assert.notNull(Assert.java:115)
并且为了使它再次正常工作,我需要将 @RunWith(SpringJUnit4ClassRunner.class)
添加到 Test class
中
@ControllerTest
@RunWith(SpringJUnit4ClassRunner.class)
public class XXControllerTest {
}
请问为什么我的@ControllerTest
注解中包含@RunWith(SpringJUnit4ClassRunner.class)
注解时无法工作?@RunWith
注解有什么特殊之处吗?还是我漏了什么东西?
附注:我对Spring配置类
使用相同的方法,它们可以正常工作。