繁体   English   中英

在JUnit Jupiter中使用自定义组合注释时,为@Autowired bean获取NPE

[英]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
}

createUser上获取NullPointerException。

如果我不使用元注释,那么它将正常工作。

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
}

您可能会缺少@Retention声明,该声明允许诸如Spring和JUnit之类的框架在运行时查看批注。

如下声明您组成的注释应该起作用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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