如何使用有条件的alloperty禁用Junit测试?

发布于 2025-01-30 04:03:45 字数 1134 浏览 1 评论 0 原文

我在服务中以环境变量作为功能标志来调节一些端点。对于一个控制器,我将 @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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2025-02-06 04:03:45

您可以添加注释测试以设置属性值。
例如

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文