我有弹簧注释的问题。我想要做的就是使用公共配置将一个必要的测试注释抓取到一个注释,并在Spring Context启动时获得空指针异常(不能自动装配bean),但是当我在每个测试类中单独使用这些注释时,一切正常。
以下是一个例子:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { JPAConfig.class, AOPConfiguration.class })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
public @interface MyTestAnnotations {
}
我希望使用@MyTestAnnotations
配置的测试用例@MyTestAnnotations
public class AspectTest {
@Autowired
PagingAndSortingBookRepository pagingAndSortingRepo;
@Autowired
SmartLoggerAspect smartLoggerAspect;
JoinPoint joinPoint;
// other methods
@Test
public void pagingTest(){
// line below throws nullPointerException
pagingAndSortingRepo.findAll(new PageRequest(1, 1));
}
}
答案 0 :(得分:2)
这是因为通过设计,您无法将@ContextConfiguration与自定义注释相结合。 see the nice explanation provided sam branan why it would not work
你应该可以使用这样的东西
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
public @interface MyTestAnnotations {
}
@MyTestAnnotations
public abstract class AbstractBaseTests
{
}
@ContextConfiguration(classes = { JPAConfig.class, AOPConfiguration.class })
public class MyTest extends AbstractBaseTests {
}