我在服务中以环境变量作为功能标志来调节一些端点。对于一个控制器,我将 @conditionalonProperty(“ enable.v2.foo”)
在班级级别和另一个
我将 @conditionalonProperty(“ enable.v2.ba.Endpoint2”)放在特定方法上。
现在,我有一些端到头的Junit 4测试,这些测试称为这些端点。签名是
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TaxServiceApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class E2ETest432 {
...
这样,所以我认为我只能将 @conditionalonProperty(“ enable.v2.ba.endpoint2”)
在方法级别和类级上的特定测试上的。
但是,如果我运行所有测试,那么也会运行THOS测试。我什至验证了它们已读取并具有正确的值:
@Value("${enable.v2.ba.endpoint2}")
private boolean switchAsBoolean;
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:dataset/....sql")
@Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:dataset/....sql")
@Test
@ConditionalOnProperty("enable.v2.ba.endpoint2")
public void shouldReturnDeliveryCostsForVehicle() throws IOException {
Assertions.assertThat(switchAsBoolean).isFalse();
...
}
此测试失败了,因为端点不存在( - >状态代码出乎意料地404),不是因为值是false!
如何在同一功能标志上调节测试?
I conditioned some endpoints in my service with environment variables as a feature flag. For one Controller I put @ConditionalOnProperty("enable.v2.foo")
at class level and for another
I put @ConditionalOnProperty("enable.v2.ba.endpoint2")
on the specific method.
Now I have some end to end junit 4 tests that call these endpoints. Signature is
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TaxServiceApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class E2ETest432 {
...
So I figured I can just put @ConditionalOnProperty("enable.v2.ba.endpoint2")
on specific tests at method level and on class level if the whole class tests an endpoint I want to switch of.
But if I run all tests, then thos tests are run as well. I even verified that they are read and have the correct value:
@Value("${enable.v2.ba.endpoint2}")
private boolean switchAsBoolean;
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:dataset/....sql")
@Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:dataset/....sql")
@Test
@ConditionalOnProperty("enable.v2.ba.endpoint2")
public void shouldReturnDeliveryCostsForVehicle() throws IOException {
Assertions.assertThat(switchAsBoolean).isFalse();
...
}
This test fails because the endpoint is not there (->status code is unexpectedly 404) not because the value is false!
How can I condition my tests on the same feature flag?
发布评论
评论(1)
您可以添加注释测试以设置属性值。
例如
@TestPropertySource(properties =“ enable.v2.ba.endpoint2 = true”)
您必须拥有2个测试类,一个带注释的true,另一个是错误的。
You can add the TestPropertySource annotation to the test to set the property value.
for example
@TestPropertySource(properties = "enable.v2.ba.endpoint2 = true")
You'd have to have 2 test classes, one annotated true and the other false.