Spring启动测试在Eclipse中失败但在命令行上传递(由于Null PropertyResolver而无法加载ApplicationContext)

时间:2016-01-07 22:59:01

标签: java eclipse maven junit spring-boot

我已经跳过了一个预先存在的项目,该项目具有无法从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内部和命令行上进行测试?任何建议都将不胜感激。

请注意我正在使用:

  1. Java 1.8
  2. Eclipse Luna Service Release 2(4.4.2)
  3. Spring Boot 1.3.0.RELEASE
  4. Spring 4.2.3.RELEASE
  5. JUnit 4.12
  6. 我的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类有一个空environment字段,它将传递给RelaxedPropertyResolver的构造函数。我尝试在Eclipse的运行配置中传递-Dspring.profiles.active=development系统变量,但这导致了相同的堆栈跟踪。

2 个答案:

答案 0 :(得分:1)

在这种情况下,问题结果是因为我在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中,它位于项目目录树中。在该行之后,所有记录的输出开始变得非常不同,直到异常发生。

答案 1 :(得分:-1)

检查类名是否以“Test”结尾,否则maven将跳过该类中的测试,这让您感到困惑。 Eclipse JUnit不关心类名并且无论如何都要运行它