0

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?

peer
  • 4,171
  • 8
  • 42
  • 73

1 Answers1

1

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.

whistling_marmot
  • 3,561
  • 3
  • 25
  • 39