简体   繁体   中英

Getting NPE for @Autowired bean when using custom composed annotation in JUnit Jupiter

TransactionalIntegrationTest.java

@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml","classpath:data.xml" })
@Tag("1")
public @interface TransactionalIntegrationTest {}

MyTestTest .java

@TransactionalIntegrationTest
public class MyTestTest {
@Autowired
protected CreateUser createUser;

@BeforeEach
public void setUp() throws Exception {
createUser.createTimesheetUser(...)} --> NPE
}

Getting NullPointerException on createUser .

If I do not use the meta annotations, then it works fine.

MyTestTest.java

@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml","classpath:data.xml" })
@Tag("1")
public class MyTestTest {
@Autowired
protected CreateUser createUser;

@BeforeEach
public void setUp() throws Exception {
createUser.createTimesheetUser(...)} --> works now
}

You're probably missing the @Retention declaration which allows frameworks like Spring and JUnit to see the annotations at runtime.

Declaring your composed annotation as follows should work.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml", "classpath:data.xml" })
@Tag("1")
public @interface TransactionalIntegrationTest {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM