无法加载ApplicationContext以JUnit测试和Spring
问题描述:
我想测试我使用的服务的JUnit,我越来越喜欢Failed to load ApplicationContext
一些错误我试图加载使用@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes={MvcConfiguration.class})
。我不停地经营业务类和Spring配置类src/main/java
文件夹春天配置类和在src/test/java
文件夹我的测试类,甚至我试图把MvcConfiguratio
n个等级中src/test/java
文件夹以及,但它没有工作,显示了同样的问题再次无法加载ApplicationContext以JUnit测试和Spring
这是我的测试类
@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");
}
}
跟踪
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: [or[email protected]2133c8f8, org.springframework.test[email protected]43a25848, org.springframewor[email protected]3ac3fd8b, org.springfra[email protected]5594a1b5, org.springframew[email protected]6a5fc7f7, org.sp[email protected]3b6eb2ec]
beforeclass
May 29, 2017 6:22:30 PM org.springframework.test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org.springframewor[email protected]3ac3fd8b] to prepare test instance [org.gov[email protected]]
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 [[email protected] 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
答
我收到此java.lang.IllegalStateException当我试图运行一些测试,但还没有完全刷新该项目(别人的,我需要一个变化已经选中)。如果你在一个团队工作,确保你已经刷新并获得最新的。 奇怪的是,测试通过命令行(mvn test)运行,但在Eclipse内部失败。
而且你高兴地忘记了包含你得到的实际错误。不仅包含错误消息。重要的信息在堆栈跟踪中。 –
这里是堆栈跟踪。可以请你帮我出来 –
你有没有试过跟踪stacktrace建议? “考虑使用@WebAppConfiguration注释您的测试类。” –