Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing with Spring and Maven: applicationContext

Seems that question old as world, but I still can't find out the solution..

I'm trying to run simple test:

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/applicationContext.xml", "/PersonsPopulateTest-context.xml"}) @Transactional public class PersonsPopulateTest { 

Files are at:

src    main       resources            applicationContext.xml 

and

src            test       resources           PersonsPopulateTest-context.xml  

So after building these files are at target/classes and target/test-classes

But mvn test command still says: Failed to load ApplicationContext

What official docs say:

@RunWith(SpringJUnit4ClassRunner.class) // ApplicationContext will be loaded from "/applicationContext.xml" and "/applicationContext-test.xml" // in the root of the classpath @ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"}) public class MyTest {     // class body... } 

Where did I go wrong?

Thanks, Vlaidimir

UPDATE. surefire-reports:

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) Caused by: java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration. at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:117) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148) ... 30 more 
like image 596
Vladimir Kishlaly Avatar asked Apr 11 '12 10:04

Vladimir Kishlaly


People also ask

How can you access the application context in a Spring integration test?

By default the ApplicationContext is loaded using the GenericXmlContextLoader which loads a context from XML Spring configuration files. You can then access beans from the ApplicationContext by annotating fields in your test class with @Autowired , @Resource , or @Inject .

What is ApplicationContext failed to load?

However, sometimes in this situation, we may encounter the application context loading error “Failed to load ApplicationContext.” This error appears in the test classes because the application context isn't loaded in the test context.


Video Answer


2 Answers

I think Maven simply didn't include the XML file from main/resources.

You could try to specify explicitly what to include in the pom.xml.

Let me know if the following configuration has worked:

    <!-- Add this directly after the <build>-opening tag -->     <resources>         <resource>             <filtering>true</filtering>             <directory>src/test/resources</directory>             <includes>                 <include>**/*.properties</include>             </includes>             <excludes>                 <exclude>**/*local.properties</exclude>             </excludes>         </resource>         <resource>             <directory>src/main/resources</directory>             <includes>                 <include>**/*.properties</include>                 <include>**/*.xml</include>             </includes>         </resource>     </resources> 

This is something I use in your case. You can edit this if you don't have properties files to be included.

like image 128
Vladimir Tsvetkov Avatar answered Oct 12 '22 17:10

Vladimir Tsvetkov


My test context file is under src\test\resources\spring folder. I managed to load the context with

@ContextConfiguration(locations={"classpath:**/test-context.xml"}) 

But reference (in test-context.xml) to the application-context.xml which is under src\main\resources\spring folder failed

I managed to load the application-context by creating a ClassPathXmlApplicationContext in the test class with

ClassPathXmlApplicationContext appContext=new ClassPathXmlApplicationContext(new String[]{"classpath:spring/application-context.xml","classpath:spring/model-context.xml"}); 

Let me know if this helps or might create any other issues.

like image 37
Ahamed Mustafa M Avatar answered Oct 12 '22 17:10

Ahamed Mustafa M