我有一个方法级监听器,如下所示
public class DefaultListener implements IInvokedMethodListener2 {
@Autowired
JdbcTemplate jdbcTemplate;
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
}
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
}
public void beforeInvocation(IInvokedMethod method, ITestResult testResult,
ITestContext context) {
updateDatabaseWithTestStartTime();
}
private void updateDatabaseWithTestStartTime() {
jdbcTemplate.update("....");
}
// other methods.
}
如何在上面的示例中 Autowiring jdbcTemplate?我研究了 spring-test 以及与 test-ng 的集成,但像 these 这样的例子,正在讨论在测试级别控制 Autowiring - 我的需求是特定于监听器的。
最佳答案
IInvokedMethodListener2
是一个 TestNG 监听器,因此与 Spring TestContext Framework 无关。
如果您想在可重用的监听器中与 Spring ApplicationContext
中的 bean 进行交互,则需要实现 Spring TestExecutionListener
。
看看SqlScriptsTestExecutionListener
获取有关如何实现此类监听器的灵感。
有关更多详细信息,请阅读 Testing 中有关“TestExecutionListener
”的所有讨论。 Spring引用手册的章节,特别注意TestExecutionListener configuration部分。
关于java - 如何将数据源注入(inject)testng方法执行监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33724354/