java - Eclipse+Junit4+SpringFramework : Running an individual Unittest class caused "Failed to load ApplicationContext" Exception

标签 java eclipse spring junit junit4

如果我在项目中运行所有单元测试类,则不会触发异常。

但是,如果我只选择一个单独的类,则会抛出“java.lang.IllegalStateException”异常,原因是“加载ApplicationContext失败”

这是堆栈跟踪:

2011-06-29 17:48:12,400 ERROR [main] - TestContextManager.prepareTestInstance Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@75c744] to prepare test instance [com.ahwkong.work.MyTest@6d6aa0]
java.lang.IllegalStateException: Failed to load ApplicationContext
      at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:203)
      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:255)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:93)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:130)
      at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
      at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
      at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
      at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
      at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [ProjectDB.xml]: Could not resolve placeholder 'env.jdbc.url'
      at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:268)
      at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:554)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:528)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:363)
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:42)
      at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:173)
      at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:199)
      ... 16 more

如您所见,根本原因是占位符分辨率。 ProjectDB.xml 文件实际上位于另一个 jar 文件中。

为什么会有这样的行为差异? (所有测试类与一个测试类)

最佳答案

env.jdbc.url 未解析。我假设您将其作为 JVM 参数(或 env 变量)传递,并且您将其记住为运行所有测试的运行配置,但它没有为单个测试设置。

确保您已从“运行”对话框中设置了所有必需的属性。

关于java - Eclipse+Junit4+SpringFramework : Running an individual Unittest class caused "Failed to load ApplicationContext" Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6526996/

相关文章:

java - 带路径/错误的 Spring Boot 休息端点

android - 日志猫。如何追踪错误?

java - 使用Spring MVC将Excel导入Mysql

java - 如何使用responseBody编写POST操作的请求

java - Jpa 几个 @ManyToOne 与 Cascade

java - 如何使用 Streams 而不是包含变量的循环?

java - 从 Jlist 保存项目

java - java9下运行jmh测试失败

c++ - 构建项目的 Eclipse CDT 问题

eclipse - gwt hibernate 程序中的异常