Spring – Junit with Spring – TestContextManager [ERROR] Caught exception while allowing TestExecutionListener

integration-testingjunitmavenspringtomcat

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:

  1. LocalTests – running in process (no server required), directly invoking my web layer methods(jersey in this case).
  2. 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!

Best Answer

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.

Related Topic