Test configuration annotations in separate annotation

I have a problem with spring annotations. All I want to do is grab whole necessary test annotations to one annotation with common config and I get null pointer exception when Spring Context starts (cannot autowire beans) but when I use those annotations separately in every test class everything works fine.

Here is an example:

@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 {
}

And test case that I want to use configuration from @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));
    }
}

Upvotes: 2

Views: 332

Answers (1)

kuhajeyan
kuhajeyan

Reputation: 11017

This is because by the design you can not combine @ContextConfiguration with custom annotation. see the nice explanation provided sam branan why it would not work

you should be able to use something like this

@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 {
  }

Upvotes: 2

Related Questions