我很难理解如何覆盖.yml文件中的属性文件。
我们使用Spring框架并使用注释(例如。@InjectMocks)
我在一个名为“one platform-properties”的配置项目YML文件中声明了一个名为paysafe fx-service.yml的属性。它设置一个名为maxRecoveryAge=0的变量。从本质上说,这是一个活生生的缓冲时间。
oneplatform:
environment: local
publisher:
rates:
maxRecoveryAge: 0
interPublishDelay: 500问题是,我希望能够在运行时在我的测试中调整这一点。将缓冲器设置为1小时、5小时和24小时。
我在测试中使用ReflectionTestUtils.setfield(PublisherClass, "maxDocumentAge", 1, int.class)调用来调整时间,但是该值不会被覆盖。当我对变量执行监视时,它在我的测试工具中工作,但是一旦测试运行深入到微服务代码中,重写的值就会丢失。对于如何使重写的值在所有测试中持续存在,有什么想法吗?
我的目标是在测试运行中使用不同的变体:
ReflectionTestUtils.setField(new FxRatesEventPublisher(),"maxRecoveryAge",1,int.class);
ReflectionTestUtils.setField(new FxRatesEventPublisher(),"maxRecoveryAge",5,int.class);
ReflectionTestUtils.setField(new FxRatesEventPublisher(),"maxRecoveryAge",24,int.class);并从本质上重写项目定义属性文件中定义的值。
发布于 2017-05-24 02:18:28
ReflectionTestUtils.setField(producerConfiguration, "messageProducer", realProducer);
Object target = AopTestUtils.getUltimateTargetObject(fxRatesEventPublisher);
int recoveryAge = -1;
ReflectionTestUtils.setField(target, "maxRecoveryAge", recoveryAge);发布于 2017-04-17 13:01:09
为什么不使用@TestPropertySource#properties
下面的示例演示如何声明内联属性。
@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" })
public class MyIntegrationTests {
// class body...
}请注意,从那个javadocs
@TestPropertySource是一个类级注释。
这意味着对于要使用的不同配置值,需要有不同的类。
发布于 2017-06-11 19:57:40
我假设您没有遵循最新的Spring约定和最佳实践,这些惯例告诉我们如何使用更喜欢基于构造函数的注入而不是基于字段的注入。
如果你的豆子是这样声明的:
public class FxRatesEventPublisher {
private final Integer maxRecoveryAge;
private final SomeDependency someDependency;
public FxRatesEventPublisher(@Value("${publisher.rate.maxRecoveryAge}") Integer maxRecoveryAge, @Autowired SomeDependency someDependency) {
this.maxRecoveryAge = maxRecoveryAge;
this.someDependency = someDependency;
}
}然后您可以像这样实例化它:
// Create an instance with your test values injected. Note that you could inject mocked dependency here as well as the real one.
FxRatesEventPublisher fxRatesEventPublisher = new FxRatesEventPublisher(24, mockDependency); 在这种情况下,测试组件要容易得多,因为可以将任何值传递给构造函数。我同意这似乎不像基于属性的注入那么漂亮,但至少值得一看。
https://stackoverflow.com/questions/43440170
复制相似问题