【问题标题】:Unit Test a configuration class not working in Multi Maven Spring Boot project对在 Multi Maven Spring Boot 项目中不起作用的配置类进行单元测试
【发布时间】:2020-02-15 11:00:30
【问题描述】:

我正在使用 SpringBoot 框架开发多模块 maven 项目。 项目分为4个模块:rest模块、service模块、repository模块、domain模块。 我正在尝试为位于服务模块中的 java 中的配置类编写单元测试。 我已经简化了案例以摆脱业务逻辑的复杂性。配置类如下:

@Configuration
@ConfigurationProperties(prefix = "x.y.feature", ignoreInvalidFields = false)
public class FeatureConfig {

        private String featureUrl;

        public String getFeatureUrl() {
            return featureUrl;
        }

        public void setFeatureUrl(String FeatureUrl) {
            this.featureUrl = featureUrl;
        }

}

属性文件是application.properties。

x.y.feature.featureUrl=featureUrl

下面是不工作的单元测试。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { FeatureConfigTest.class })
public class FeatureConfigTest {
    @Autowired
    private FeatureConfig featureConfig;

    @Test
    public void testgetFeatureUrl() {
        String expected ="featureUrl";
        assertEquals(expected,featureConfig.getFeatureUrl());
    }
}

当我运行单元测试时,它会抛出以下异常:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)

【问题讨论】:

    标签: java spring-boot unit-testing


    【解决方案1】:

    问题是您正在尝试使用 FeatureConfig 的对象 从应用程序上下文。您尚未初始化应用程序上下文。 当您使用 @Autowire 注释时,您正在映射字段“featureConfig” FeatureConfig 类的实例位于应用程序上下文中。 要绕过此错误,您需要初始化应用程序上下文。

    首先,在测试类中创建静态类,这将帮助我们不加载 整个应用程序上下文。因为对于这个测试用例,您不需要加载整个应用程序。 首先,在测试类中创建静态类,这将帮助我们不加载 整个应用程序上下文。因为对于这个测试用例,您不需要加载整个应用程序。

        @EnableConfigurationProperties(FeatureConfig.class)
        public static class TestConfiguration {
        }
    

    之后,您启动应用程序上下文,但将静态类作为配置传递 你创造了。这样做不是为了加载整个应用程序上下文。

    @SpringBootTest(classes = { FeatureConfigTest.TestConfiguration class })

    将以下更改复制并粘贴到您的测试类中,一切都会正常。

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = { FeatureConfigTest.TestConfiguration class })
    public class FeatureConfigTest {
        @Autowired
        private FeatureConfig featureConfig;
    
        @Test
        public void testgetFeatureUrl() {
            String expected ="featureUrl";
            assertEquals(expected,featureConfig.getFeatureUrl());
        }
    
        @EnableConfigurationProperties(FeatureConfig.class)
        public static class TestConfiguration {
        }
    }
    

    【讨论】:

    • 非常感谢。它有助于解决问题。但我仍然面临另一个问题。当我运行单元测试方法featureConfig.getFeatureUrl() 它返回null。它应该返回应用程序属性文件中指定的值。
    • 这个问题可能发生在以下情况:1-没有application.properties文件,2属性不在application.properties文件中,3-测试类找不到应用程序属性文件。我要做的是在 test/resources 文件夹中检查是否有 application.properties 类,然后检查是否是属性。最后在你的测试类中添加@TestPropertySource(locations="classpath:application.properties")
    • 你说得对,我忘记在测试文件夹中添加 application.properties。它是在休息模块中添加的,而不是在服务模块中。我不需要@TestPropertySource(locations="classpath:application.properties")。谢啦。你帮了很多忙。
    猜你喜欢
    • 2020-04-10
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2018-03-21
    相关资源
    最近更新 更多