当前位置: 首页 > 知识库问答 >
问题:

Spring Test中未加载配置属性

乜建柏
2023-03-14

我有一个Spring Boot应用程序,它有一些配置属性。我正在尝试为一些组件编写测试,并希望从< code>test.properties文件中加载配置属性。我不能让它工作。

这是我的代码:

测试。属性文件(在src/test/resources下):

vehicleSequence.propagationTreeMaxSize=10000

配置属性类:

package com.acme.foo.vehiclesequence.config;

import javax.validation.constraints.NotNull;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = VehicleSequenceConfigurationProperties.PREFIX)
public class VehicleSequenceConfigurationProperties {
    static final String PREFIX = "vehicleSequence";

    @NotNull
    private Integer propagationTreeMaxSize;

    public Integer getPropagationTreeMaxSize() {
        return propagationTreeMaxSize;
    }

    public void setPropagationTreeMaxSize(Integer propagationTreeMaxSize) {
        this.propagationTreeMaxSize = propagationTreeMaxSize;
    }
}

我的测试:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = VehicleSequenceConfigurationProperties.class)
@TestPropertySource("/test.properties")
public class VehicleSequenceConfigurationPropertiesTest {

    @Autowired
    private VehicleSequenceConfigurationProperties vehicleSequenceConfigurationProperties;

    @Test
    public void checkPropagationTreeMaxSize() {
        assertThat(vehicleSequenceConfigurationProperties.getPropagationTreeMaxSize()).isEqualTo(10000);
    }
}

测试失败,并显示“预期实际不为空”,这意味着未设置配置属性类中的属性传播TreeMaxSize

共有1个答案

燕烨
2023-03-14

发布问题两分钟后,我找到了答案。

我必须启用具有@EnableConfigurationProperties的配置属性(VehicleSequenceConfigurationProperties.class)

@RunWith(SpringRunner.class)
@TestPropertySource("/test.properties")
@EnableConfigurationProperties(VehicleSequenceConfigurationProperties.class)
public class VehicleSequenceConfigurationPropertiesTest {

    @Autowired
    private VehicleSequenceConfigurationProperties vehicleSequenceConfigurationProperties;

    @Test
    public void checkPropagationTreeMaxSize() {
        assertThat(vehicleSequenceConfigurationProperties.getPropagationTreeMaxSize()).isEqualTo(10000);
    }
}
 类似资料:
  • 你能帮我找出配置中缺少的步骤吗? 我正试图将logger添加到我非常简单的web应用程序中:为了做到这一点,我使用了log4j2 (beta9)。 我写下了我的<code>log4j2.xml<code>如下 我把它放在WEB-INF文件夹里。 然后,我有一个简单的servlet,它执行以下操作 正如你所看到的,这只是一个尝试,看看是否如预期的那样工作,但它没有。通过阅读此处的web app,我不

  • 在我的spring boot 2应用程序中,我有一个应用程序。yml as, 我的配置类是, 代码在本地计算机中运行良好,所有值都按预期加载。但cloud env Consor将yml文件序列化为环境变量,因此我的配置表示为, 这是一个字符串。我不知道Consor以何种格式表示/序列化值。现在我的DefaultRateLimitsConfig无法加载,因为它会出错, 如何以更干净的方式在两个环境中

  • 我正在为我的Spring REST项目建立swagger2文档。但是当我尝试执行http://localhost:8085/swagger-ui.html时,返回空页面。问题是我的bean类没有被我的spring MVC应用程序加载,它也不是Spring Boot应用程序。 下面是我的swagger2配置类 请同时找到web.xml条目 如果有什么东西丢了,请告诉我?任何帮助都是非常感谢的。

  • 我正在尝试从YML加载配置。如果这些是逗号分隔的值,我可以加载值,也可以加载列表。但我无法加载典型的YML列表。 配置类 工作路线。yml 不工作。yml 为什么我不能加载yml的第二个版本/我有一个语法错误? 环境:科特林,Spring2.0.0。M3

  • 我正在为我的Spring启动应用程序编写联调,但当我尝试使用@TestProperty tySource覆盖一些属性时,它正在加载上下文xml中定义的属性文件,但不会覆盖注释中定义的属性。

  • 我有两个项目,CarpoolDB和拼车。 CarpoolDB:包含后端的东西 拼车应用程序上下文。xml server.properties 我做了一罐carpoolDB和地方拼车应用程序 拼车:包含UI东西和后端联系人carpoolDB jar,并具有 carpool-application-context1.xml spring-servlet.xml 拼车。性质 现在,我有一个类com.on