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:
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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With