How to initialize Spring application context file in Selenium TestNG environment

Viewed 236

How to initialize a Spring application context File in Selenium TestNG environment in Spring 5?

I tried the following code, but it's using classpathapplicationcontext, so every build will delete the application context file.

 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;
    }
}

It works in Eclipse, but not in IntelliJ.

1 Answers

As far as I understood your question, without test code snippets, you should change the test classes as follows:

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

Annotation ContextConfiguration configures how to load Spring context for integration tests and AbstractTestNGSpringContextTests class integrates the Spring TestContext Framework with explicit ApplicationContext testing support in a TestNG environment.

Related