【问题标题】:How to initialize Spring application context file in Selenium TestNG environment如何在 Selenium TestNG 环境中初始化 Spring 应用程序上下文文件
【发布时间】:2021-01-18 00:39:13
【问题描述】:

如何在 Spring 5 的 Selenium TestNG 环境中初始化一个 Spring 应用上下文文件?

我尝试了以下代码,但它使用的是 classpathapplicationcontext,所以每次构建都会删除应用程序上下文文件。

 public class SpringInitManager {
        private ClassPathXmlApplicationContext ctx;

    public void init() {// "classpath:applicationContext.xml"
        setCtx(new ClassPathXmlApplicationContext("classpath:applicationContext.xml"));
    }
    // 
    public ClassPathXmlApplicationContext getCtx() {
        return ctx;
    }
    // 
    public void setCtx(ClassPathXmlApplicationContext ctx) {
        this.ctx = ctx;
    }
}

它适用于 Eclipse,但不适用于 IntelliJ。

【问题讨论】:

  • “我试过下面的代码,但它使用的是classpathapplicationcontext,所以每次构建都会删除应用程序上下文文件”是什么意思?因为如果您将文件放在正确的位置,这不是它的工作方式。请准确描述您在做什么以及您的文件在哪里。
  • 请分享更多代码 sn-ps 以了解您要实现的测试用例

标签: java spring maven intellij-idea


【解决方案1】:

据我了解你的问题,没有测试代码sn-ps,你应该改变测试类如下:

@ContextConfiguration("classpath:applicationContext.xml")
public class TestNGSpringTest extends AbstractTestNGSpringContextTests {
   //testNG test methods...
}

注解ContextConfiguration 配置如何为集成测试加载Spring 上下文,AbstractTestNGSpringContextTests 类将Spring TestContext 框架与TestNG 环境中的显式ApplicationContext 测试支持集成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2014-02-13
    • 2010-09-23
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 2010-10-24
    相关资源
    最近更新 更多