java - 在 JUnit Jupiter 中使用自定义组合注释时为 @Autowired bean 获取 NPE
问题描述
TransactionalIntegrationTest.java
@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml","classpath:data.xml" })
@Tag("1")
public @interface TransactionalIntegrationTest {}
我的测试测试.java
@TransactionalIntegrationTest
public class MyTestTest {
@Autowired
protected CreateUser createUser;
@BeforeEach
public void setUp() throws Exception {
createUser.createTimesheetUser(...)} --> NPE
}
在createUser
.
如果我不使用元注释,那么它工作正常。
我的TestTest.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 {
}
推荐阅读
- next.js - Next.js 是否可以在使用域路由时禁用子文件夹路由?
- python - Python - valuecounts() 方法 - 显示所有结果
- wordpress - 编辑 WooCommerce 模板文件,不受未来更新的影响
- vue.js - 您可以将变量传递给 Vuetify 组件的“颜色”字段吗?
- python - 如何在数据框中删除重复项并首先保留两个例外?
- python - 在 Python 中定义“api”回溯错误
- firebase - 如何使用 FCM 在 Flutter 中实现推送通知?
- javascript - 从数组中获取每个第 n 个元素并映射/推送到新数组
- bash - 根据列名打印列
- nlp - GCP AutoML Natural Language 中 Word Boundary 的 PDF 标注问题