gpt4 book ai didi

java - 我应该在测试期间模拟 Spring Cloud Config 服务器属性吗?

转载 作者:搜寻专家 更新时间:2023-11-01 01:09:45 24 4
gpt4 key购买 nike

如何测试将 spring cloud 配置服务器的属性作为依赖项注入(inject)其中的服务?

  1. -我是否在测试期间使用 new 关键字简单地创建自己的属性?(new ExampleProperties())
  2. 或者我是否必须使用 spring 并创建某种测试属性并使用配置文件来告知要使用哪些属性?
  3. 还是应该让 spring 在测试期间调用 spring cloud 配置服务器?

我的服务如下所示:

@Service
class Testing {

private final ExampleProperties exampleProperties

Testing(ExampleProperties exampleProperties) {
this.exampleProperties = exampleProperties
}

String methodIWantToTest() {
return exampleProperties.test.greeting + ' bla!'
}
}

我的项目在启动期间调用 spring 云配置服务器以获取属性,这是通过在 bootstrap.properties 中启用以下内容来启用的:

spring.cloud.config.uri=http://12.345.67.89:8888

我的配置如下所示:

@Component
@ConfigurationProperties
class ExampleProperties {

private String foo
private int bar
private final Test test = new Test()

//getters and setters

static class Test {

private String greeting

//getters and setters
}
}

属性文件如下所示:

foo=hello
bar=15

test.greeting=Hello world!

最佳答案

您可以使用 @TestPropertySource annotation在测试期间伪造属性:

@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" })
public class MyIntegrationTests {
// class body...
}

关于java - 我应该在测试期间模拟 Spring Cloud Config 服务器属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44453555/

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