4
votes

I want to test my service using JUnit,I am getting some error like Failed to load ApplicationContext I tried to load spring configuration class using @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes={MvcConfiguration.class}).I kept bussiness classes and spring configuration class in src/main/java folder and my test classes in src/test/java folder even i tried to put MvcConfiguration class in src/test/java folder as well but it didn't work out shows same problem again

This is my test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, 
classes={MvcConfiguration.class})
public class ServiceTest {
    @Autowired
   private EmployeeService empService;
    @Test
    public void test() {
        assertEquals(21, empService.employeeCount());
    }
    @BeforeClass
    public static void serviceIntaciation(){
        System.out.println("beforeclass");
    }

}

Trace

May 29, 2017 6:22:30 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
INFO: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
May 29, 2017 6:22:30 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@2133c8f8, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@43a25848, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3ac3fd8b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@5594a1b5, org.springframework.test.context.transaction.TransactionalTestExecutionListener@6a5fc7f7, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@3b6eb2ec]
beforeclass
May 29, 2017 6:22:30 PM org.springframework.test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3ac3fd8b] to prepare test instance [org.gov.mord.nrlm.test.ServiceTest@13fee20c]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException: Cannot load WebApplicationContext from non-web merged context configuration [MergedContextConfiguration@64616ca2 testClass = ServiceTest, locations = '{}', classes = '{class org.gov.mord.nrlm.test.MvcConfiguration}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.web.AnnotationConfigWebContextLoader', parent = [null]]. Consider annotating your test class with @WebAppConfiguration.
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:109)
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:61)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 26 more
1
And you happily forget to include the actual error you get. Don't only include the error message. The important information is in the stack trace.M. Deinum
here is the stack trace .Can you please help me outSushil Panwar
have you tried to follow the stacktrace advise? "Consider annotating your test class with @WebAppConfiguration."Ilya Zinkovich
@llya thanks for helping me out @WebAppConfiguration works for me. in addition I need to change the version of 'javax.servlet-api' version from 2.0.4 to 3.0.1 in pom.xml fileSushil Panwar

1 Answers

0
votes

I received this java.lang.IllegalStateException when I tried running some tests but hadn't fully refreshed the project (someone else had checked in a change that I needed). If you're working on a team, ensure that you have refreshed and gotten latest. Oddly enough, the tests ran via command line (mvn test) but failed inside Eclipse.