java - 扩展 AbstractTestNGSpringContextTests 时,抽象基类中的自动装配弹簧依赖项始终为空
问题描述
我有一个抽象的测试基类,它扩展自AbstractTestNGSpringContextTests
:
@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
@ActiveProfiles(resolver = ActiveProfileResolver.class)
public abstract class BaseTest extends AbstractTestNGSpringContextTests {
@Autowired
private MyActions myActions;
@BeforeSuite(dependsOnMethods = "beforeSuite", alwaysRun = true)
public void beforeUISuite() {
myActions.doSomething();
}
}
以及一个从BaseTest
public class ExampleTest extends BaseTest {
@Test
public void exampleTest( ){
// Do something
}
}
当beforeUISuite
方法运行时,myActions
始终为空。为什么这不是由 Spring 自动装配的?测试运行时正确设置了自动连接的依赖项ExampleTest
,但是beforeUISuite
执行时,什么都没有连接?
如果我将myActions
protected 和注释掉beforeUISuite
,myActions
则在测试运行时正确实例化。在标记的方法执行之前似乎没有连接@BeforeSuite
?
解决方案
推荐阅读
- javascript - Nest js中使用中间件时如何给中间件内部的use函数传参数
- python - 强制 HTML 显示来自 Python 模型对象的秒数
- c - c语言中的词法作用域是什么?
- php - ckeditor 5中的图像上传不适用于laravel 8
- c++ - 最小异或问题,超过时间限制,在少数情况下给出不同的答案
- javascript - 将按钮的 ID 传递给我的模态文本框
- python - Python/Numpy:获取二维矩阵中的前 k 个最大值作为掩码
- opencv - opencv写视频的速度是不是比实际设置速度慢?
- reactjs - 如何在 React 的 ProductPage 链接中获取产品 ID?
- python - python 的 file.read() 不返回文本文件中的字符串