gpt4 book ai didi

java - Spring Boot 测试在 Eclipse 中失败,但在命令行上通过(由于 PropertyResolver 为 Null,无法加载 ApplicationContext)

转载 作者:行者123 更新时间:2023-12-02 08:48:24 25 4
gpt4 key购买 nike

我已经跳到了一个预先存在的项目,该项目的 Spring JUnit 测试无法从 Eclipse 内部成功执行,但当我执行 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 内部和命令行上工作?任何建议将不胜感激。

请注意,我正在使用:

  1. Java 1.8
  2. Eclipse Luna 服务版本 2 (4.4.2)
  3. Spring Boot 1.3.0.RELEASE
  4. Spring 4.2.3.RELEASE
  5. JUnit 4.12

我的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.propsmyclient.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 类有一个 null environment 字段,该字段正在传递给 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 中,它位于项目目录树中。在该行之后,所有记录的输出开始变得非常不同,直到发生异常。

关于java - Spring Boot 测试在 Eclipse 中失败,但在命令行上通过(由于 PropertyResolver 为 Null,无法加载 ApplicationContext),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34666753/

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