In my Spring-Maven--Hibernate-Mysql runnint on Tomcat web app I'm running 2 types of Junit integration tests using 2 different Junit categories:
- LocalTests - running in process (no server required), directly invoking my web layer methods(jersey in this case).
- HttpTests - Simulating a client and calling my web-layer via an http request, requires tomcat up and running.
Above each test class I have:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })
And my test suite looks like that (this is one of 2, I have one for each category):
@RunWith(Categories.class)
@IncludeCategory(HttpTest.class)
@SuiteClasses({ ...All My Test Classes... })
public class HttpSuiteITCase {
// Excluded: NotificationTests, ImageHttpTests
/**
* Run once before any of the test methods.
*/
@BeforeClass
public static void setTestsConfigurations() {
TestConfiguration.setup(false);
}
My testApplicationContext is actually empty, it only contains the component scan:
<context:component-scan base-package="com.company.app" />
As I run my local tests it everything works smoothly, but as I'm invoking mt HTTP tests it crashes:
2012-07-22 17:56:13 DefaultListableBeanFactory [INFO] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2598a35d: defining beans [httpBrandManagerTestsProxy,httpClubTestsProxy,<HERE THERE'S A BUNCH OF SPRING BEANS>,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,contextApplicationContextProvider,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
2012-07-22 17:56:13 TestContextManager [ERROR] Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@29565e9d] to prepare test instance [integrationTests.http.tests.UserLoginDataHttpTests@480d41f3]
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)
I tried tons of stuff nothing works :( My testApplicationContext is under the test/resources folder.
I have noticed that the exception marks a specific class: UserLoginDataHttpTests. But I can't see anything special about this class, just a regular spring bean.
Thanks in advance!
4 Answers
The problem occurs since Spring context gets loaded before one of my listeners, which is defined in web.xml. Spring is initializing some beans that uses non-spring classes that gets initialized using my own listener. To solve the issue I should make sure my listener runs first.
For me adding in VM Options worked.
Added this to VM Options for that test case: -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl -Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
Apart from the above mentioned answer. You should also look for "caused by" to see if there is any dependency resolution problem. For example, if your class under test is MyService.java and it uses a dependency MyRepository.java. Then you should make sure all such dependencies are either Mocked or Autowired in your test class.
Super late, but I just got this error. In my case I'd updated a number of dependencies. For me the solution was to do a Maven -> Update Project in eclipse.