Spring @Value 读取的继承属性为空
Inherited properties read by Spring @Value are null
我正在尝试使用 spring 通过 @Value 在 child class 中读取的一些属性。这些属性在 parent class 中被正确读取,但在 child 中最终为空。我使用的是注释配置而不是 xml 配置。
例如,我正在尝试在扩展 WebTemplate 的 TestListeners 中使用来自 WebTemplate 的 属性 reportType:
@ContextConfiguration(classes = AppConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class WebTemplate extends AbstractTestNGSpringContextTests {
@Autowired
protected SeleniumDriver driver;
@Value("${environment.url}")
protected String environmentUrl;
@Value("${report.type}")
protected String reportType;
@Value("${auto.open.report}")
protected String autoOpenReport;}
这里的属性最终为空。如果我在 WebTemplate 中使用它们,一切都很好并且可以工作。
public class TestListener extends WebTemplate implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
System.out.println(reportType);
System.out.println(autoOpenReport);
我还考虑过 Webtemplate Object 在测试周期的某个时候被破坏,并试图在每次测试开始时将它们存储在 child class 上,从 super 调用它们。这也不起作用:
public class TestListener extends WebTemplate implements ITestListener {
private String reportType;
private String autoOpenReport;
@Override
public void onTestStart(ITestResult result) {
this.reportType = super.reportType;
this.autoOpenReport = super.autoOpenReport;
有什么建议吗?
更新: 经过一番调查发现问题实际上是由spring引起的。 Class WebTemplate 注释为 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
。根据我的理解,这会在每次测试之后和进入听众之前立即破坏整个 spring 环境。因此,当进入各自的 testNg 侦听器时,spring 管理的所有内容都消失了。
我的兴趣是在测试 类 中只有 PageObjects Autowired,所以我最终从 spring 环境中删除了所有不是 PageObject 的东西。
您有 2 个 class,每个扩展 WebTemplate
,但其中一个不受 Spring 管理,因此不会得到 injected/processed。事实上,您扩展由 Spring 管理的 class 并不会自动使创建的实例由 Spring 管理。
您有 2 个 class,因此有 2 个不同的对象,它们不共享相同的 WebTemplate
属性。它们是 2 个不同的 classes,因此是实例。
我正在尝试使用 spring 通过 @Value 在 child class 中读取的一些属性。这些属性在 parent class 中被正确读取,但在 child 中最终为空。我使用的是注释配置而不是 xml 配置。
例如,我正在尝试在扩展 WebTemplate 的 TestListeners 中使用来自 WebTemplate 的 属性 reportType:
@ContextConfiguration(classes = AppConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class WebTemplate extends AbstractTestNGSpringContextTests {
@Autowired
protected SeleniumDriver driver;
@Value("${environment.url}")
protected String environmentUrl;
@Value("${report.type}")
protected String reportType;
@Value("${auto.open.report}")
protected String autoOpenReport;}
这里的属性最终为空。如果我在 WebTemplate 中使用它们,一切都很好并且可以工作。
public class TestListener extends WebTemplate implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
System.out.println(reportType);
System.out.println(autoOpenReport);
我还考虑过 Webtemplate Object 在测试周期的某个时候被破坏,并试图在每次测试开始时将它们存储在 child class 上,从 super 调用它们。这也不起作用:
public class TestListener extends WebTemplate implements ITestListener {
private String reportType;
private String autoOpenReport;
@Override
public void onTestStart(ITestResult result) {
this.reportType = super.reportType;
this.autoOpenReport = super.autoOpenReport;
有什么建议吗?
更新: 经过一番调查发现问题实际上是由spring引起的。 Class WebTemplate 注释为 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
。根据我的理解,这会在每次测试之后和进入听众之前立即破坏整个 spring 环境。因此,当进入各自的 testNg 侦听器时,spring 管理的所有内容都消失了。
我的兴趣是在测试 类 中只有 PageObjects Autowired,所以我最终从 spring 环境中删除了所有不是 PageObject 的东西。
您有 2 个 class,每个扩展 WebTemplate
,但其中一个不受 Spring 管理,因此不会得到 injected/processed。事实上,您扩展由 Spring 管理的 class 并不会自动使创建的实例由 Spring 管理。
您有 2 个 class,因此有 2 个不同的对象,它们不共享相同的 WebTemplate
属性。它们是 2 个不同的 classes,因此是实例。