[英]Spring Boot Test Fails in Eclipse but passes on command line (failed to load ApplicationContext because of Null PropertyResolver)
我已經跳過了一個預先存在的項目,該項目具有無法從Eclipse內部成功執行的Spring JUnit測試,但是當我執行mvn clean install
時,它在命令行上運行。 我真的希望能夠從Eclipse內部執行我的測試,以便我可以在適當的條件下輕松地遠程調試我的Spring應用程序。 我已經嘗試過谷歌搜索並搜索預先存在的StackOverflow問題,但令人驚訝的是似乎沒有人有我的堆棧跟蹤。
堆棧跟蹤的要點是:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: java.lang.IllegalArgumentException: PropertyResolver must not be null
...
如何使用@SpringJUnit4ClassRunner
和@SpringApplicationConfiguration
注釋在Eclipse內部和命令行上進行測試? 任何建議將不勝感激。
請注意我正在使用:
我的Java測試類如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MyApplication.class})
public class MyTest {
// This test is extremely simplistic. I commented out everything I could
// in Eclipse where I am still able to reproduce the problem.
@Test
public void passMe() {
// pass this test :)
}
}
MyApplication
的代碼如下:
@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@EnableScheduling
@PropertySource({"classpath:myservice.props", "classpath:myclient.props"})
public class MyApplication {
private static final Logger LOG =
LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
SpringApplication app =
new SpringApplication(MyApplication.class);
app.setBannerMode(Banner.Mode.LOG);
app.setBannerMode(Banner.Mode.CONSOLE);
ApplicationContext ctx = app.run(args);
if (LOG.isTraceEnabled()) {
LOG.trace("Application Name:", ctx.getApplicationName());
LOG.trace("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
LOG.trace(beanName);
}
}
}
//To resolve ${} in @Value
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfig() {
return new PropertySourcesPlaceholderConfigurer();
}
@PreDestroy
public void finalizeExit() {
LOG.warn("Application exiting...");
if (LOG.isTraceEnabled()) {
DumpEndpoint dump = new DumpEndpoint();
LOG.trace("thread dump info: {}", dump.invoke());
}
ShutdownEndpoint shutdown = new ShutdownEndpoint();
LOG.warn("Shutingdown gracefully...");
shutdown.invoke();
}
}
myservice.props
和myclient.props
文件位於Maven項目的src/main/resources
文件夾中。 另外,我已經明確地將src/main/resources
文件夾添加到測試運行配置的類路徑中但無濟於事。
從Eclipse運行MyTest
時,我得到的完整堆棧跟蹤如下:
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:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
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.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:193)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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:675)
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: PropertyResolver must not be null
at org.springframework.util.Assert.notNull(Assert.java:115)
at org.springframework.boot.bind.RelaxedPropertyResolver.<init>(RelaxedPropertyResolver.java:42)
at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.getExcludeAutoConfigurationsProperty(EnableAutoConfigurationImportSelector.java:165)
at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.getExclusions(EnableAutoConfigurationImportSelector.java:152)
at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.selectImports(EnableAutoConfigurationImportSelector.java:80)
at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:328)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:233)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:154)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:131)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:225)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:154)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:140)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:282)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)
at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:295)
at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 25 more
再次感謝你的幫助。
編輯:發生異常是因為EnableAutoConfigurationImportSelector
類具有空environment
字段,它將傳遞給RelaxedPropertyResolver
的構造RelaxedPropertyResolver
。 我嘗試在Eclipse的運行配置中傳遞-Dspring.profiles.active=development
系統變量,但這導致了相同的堆棧跟蹤。
在這種情況下,問題結果是因為我在Eclipse中的項目依賴於下游項目。 當我進入項目的構建路徑並刪除下游項目時,我的測試從紅色變為綠色。
通過對比正確的Maven CLI執行輸出和不正確的Eclipse執行來發現差異。 通過在Maven CLI和Eclipse之間區分命令行輸出,我在Maven CLI控制台輸出中發現了以下行:
<org.springframework.core.io.support.PathMatchingResourcePatternResolver><main><org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingJarResources(?:?):Looking for matching resources in jar file [file:/Users/jthoms/.m2/repository/com/acme/teamnamespace/downstream-project/0.0.2/downstream-project-0.0.2.jar]>
這對應於Eclipse控制台輸出中的一行:
<org.springframework.core.io.support.PathMatchingResourcePatternResolver><main><org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindMatchingFileSystemResources(?:?):Looking for matching resources in directory tree [/git/cloned/downstream-project/target/classes/com/acme/teamnamespace]>
在Maven CLI中輸出來自JAR文件的源,但是在Eclipse中它是在項目目錄樹中。 在該行之后,所有記錄的輸出開始變得非常不同,直到發生異常。
檢查類名是否以“Test”結尾,否則maven將跳過該類中的測試,這讓您感到困惑。 Eclipse JUnit不關心類名,無論如何都要運行它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.