【问题标题】:Upgrade Spring Boot 2.0.8 to 2.1.2 - Failed to load ApplicationContext in tests将 Spring Boot 2.0.8 升级到 2.1.2 - 无法在测试中加载 ApplicationContext
【发布时间】:2019-02-04 09:21:55
【问题描述】:

当将 spring 从 2.0.8 升级到 2.1.2(使用 JDK 8)时,应用程序启动并运行良好,但由于 java.lang.IllegalStateException: Failed to load ApplicationContext 而测试失败。

我正在使用一些测试扩展的抽象类。

@SpringBootTest
@RunWith(SpringRunner.class)
public abstract class AbstractTestkonfiguration {

  @TestConfiguration
  static class TestEnvironmentConfiguration {
    @Component
    @PropertySource(value = "classpath:my-test.properties")
    @ConfigurationProperties(prefix = "my")
    public static class MyTestProperties extends EnvironmentalProperties {
    }
  }

}

EnvironmentalProperties 类是类型安全配置属性的类 (Doc)

在有效的升级和提供一类 EnvironmentalProperties 之前,但现在我得到了一个

[...]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.abc.EnvironmentalProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1651)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    ... 90 more

这可能与嵌套配置类检测 (Upgrading to Spring Framework 5.x) 的更改有关吗?如果是这样,我如何配置一个仅用于测试的 EnvironmentalProperties bean?

更新:即使如下使用它也不起作用(相同的结果)。

@SpringBootTest
@RunWith(SpringRunner.class)
public abstract class AbstractTestkonfiguration {

  @Configuration
  public static class TestEnvironmentConfiguration {
    @Bean
    public MyTestProperties environmentalProperties(){
        return new EnvironmentalProperties() {
            // manual creation of an instance
        }
    }
  }

}

【问题讨论】:

    标签: spring java-11


    【解决方案1】:

    您必须进行一些更改。

    • 您尚未通过@EnableConfigurationProperties 启用配置属性
    • 属性源需要在测试类中注入
    • 移除@Component注解

    这是一个工作示例;

    src/test/resources/my-test.properties

    my.server.name=foo
    my.server=test
    

    src/main/resources/application.properties

    my.name=production
    

    生产配置。

    @ConfigurationProperties(prefix = "my")
    @PropertySource(value = "classpath:application.properties")
    public class EnvironmentalProperties {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(final String name) {
            this.name = name;
        }
    }
    
    
    @SpringBootTest
    @TestPropertySource(value = {"classpath:my-test.properties", "classpath:application.properties"})
    @RunWith(SpringRunner.class)
    public class AbstractTestkonfiguration {
        @Autowired
        private MyTestProperties myTestProperties;
    
    
        @TestConfiguration
        @EnableConfigurationProperties(MyTestProperties.class)
        public static class TestEnvironmentConfiguration {
            @ConfigurationProperties(prefix = "my")
            public static class MyTestProperties  extends EnvironmentalProperties {
                private String server;
    
                public String getServer() {
                    return server;
                }
    
                public void setServer(final String server) {
                    this.server = server;
                }
            }
        }
    
        @Test
        public void check_configuration () {
            Assert.assertEquals(myTestProperties.getServer(), "test");
            Assert.assertEquals(myTestProperties.getName(), "production");
        }
    

    这适用于 Java 11 和 spring-boot 2.1.2.RELEASE。请注意,这只是一个示例。您必须适当地调整它以适应您的项目。

    【讨论】:

    • 谢谢!这样可行。与帖子中的第一个示例相比,我做了以下更改:a) 在班级级别添加了 @TestPropertySource(value = {"classpath:my-test.properties"})@EnableConfigurationProperties(AbstractTestkonfiguration.TestEnvironmentConfiguration.MyTestProperties.class)。 2) 我在配置属性类中删除了@Component@PropertySource(value = "classpath:my-test.properties")。但是,它不适用于 @EnableConfigurationProperties 级别的 TestEnvironmentConfiguration
    • 很高兴有帮助。这是正确的@EnableConfigurationProperties 应该是TestEnvironmentConfiguration。顺便说一句,我在测试课上提到了@TestPropertySource
    猜你喜欢
    • 2020-09-26
    • 2019-01-30
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多