在单独的注释中测试配置注释
Test configuration annotations in separate annotation
我对 spring 注释有疑问。我想要做的就是将整个必要的测试注释获取到一个具有通用配置的注释,并且当 Spring 上下文启动时我得到空指针异常(不能自动装配 bean)但是当我在每个测试中单独使用这些注释时 class 一切正常
这是一个例子:
@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));
}
}
这是因为根据设计,您无法将 @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 {
}
我对 spring 注释有疑问。我想要做的就是将整个必要的测试注释获取到一个具有通用配置的注释,并且当 Spring 上下文启动时我得到空指针异常(不能自动装配 bean)但是当我在每个测试中单独使用这些注释时 class 一切正常
这是一个例子:
@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));
}
}
这是因为根据设计,您无法将 @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 {
}