mardi 11 septembre 2018

AbstractApplicationContext has not been refreshed yet exception

I have a standalone jar application which uses spring context as below:

class MainClass {    
    public static void main(final String[] args) {
        AbstractApplicationContext applicationContext = new 
        GenericXmlApplicationContext("file-path-to-xml-config");
        ClassA classA = applicationContext.getBean(ClassA.class);
        ...
    } 
}

Am trying to integration-test above method as below:

@ContextConfiguration(locations = { "file-path-to-test-xml-config" })
public class AbstractIT extends AbstractTestNGSpringContextTests {
    @Inject
    private ClassB classB;

    @Test
    public void testMain() {
        classB.someMethod();
        MainClass.main(null);
    }
}

Above setup throws below exception:

java.lang.IllegalStateException: org.springframework.context.support.AbstractApplicationContext__Generated_OpenPojo@356f20b7 has not been refreshed yet

It seems like creating context from within test case as well as from within main is clashing and thus it is throwing an exception while instantiating it from main method.

I don't want to change spring initialization from within main method as this is a standalone application. Also, I wanted to load spring context in test as am loading test-context in the test.

Is there a way I can get rid of this exception?

Aucun commentaire:

Enregistrer un commentaire