gpt4 book ai didi

spring-mvc - 无法为 Junit 测试和 Spring 加载 ApplicationContext

转载 作者:行者123 更新时间:2023-12-03 14:21:48 25 4
gpt4 key购买 nike

我想使用 JUnit 测试我的服务,我收到一些错误,如 Failed to load ApplicationContext我尝试使用 @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class,
classes={MvcConfiguration.class})
加载 spring 配置类.我在src/main/java中保留了业务类和spring配置类文件夹和我在 src/test/java 中的测试类文件夹甚至我试图放 MvcConfiguratio n 类(class) src/test/java文件夹也是如此,但它没有解决再次显示同样的问题

这是我的测试课

@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: [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

最佳答案

当我尝试运行一些测试但没有完全刷新项目(其他人已 checkin 我需要的更改)时,我收到了这个 java.lang.IllegalStateException。如果您在团队中工作,请确保您已更新并获得最新信息。
奇怪的是,测试是通过命令行(mvn test)运行的,但在 Eclipse 中失败了。

关于spring-mvc - 无法为 Junit 测试和 Spring 加载 ApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44236516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com