0

I'm trying to do some integration testing for my cloud streaming application. One of the main issues I'm observing so far is that the TestChannelBinderConfiguration keeps picking up the configuration specified in src/main/java/resources/application.yml instead of keeping it as blank (since there is no config file in /src/test/resources/).

If I delete the application.yml file or remove all spring-cloud-stream related configuration, the test passes. How can I ensure that the TestChannelBinderConfiguration does not pick up application.yml file.

  @Test
  public void echoTransformTest() {
    try (ConfigurableApplicationContext context =
        new SpringApplicationBuilder(
              TestChannelBinderConfiguration.getCompleteConfiguration(DataflowApplication.class))
            .properties(new Properties())
            .run("--spring.cloud.function.definition=echo")) {
      InputDestination source = context.getBean(InputDestination.class);
      OutputDestination target = context.getBean(OutputDestination.class);

      GenericMessage<byte[]> inputMessage = new GenericMessage<>("hello".getBytes());

      source.send(inputMessage);
      assertThat(target.receive().getPayload()).isEqualTo("hello".getBytes());
    }
  }
tsar2512
  • 2,826
  • 3
  • 33
  • 61
  • Don't add `@SpringBootTest` to the test class? – Gary Russell Nov 18 '20 at 14:46
  • @GaryRussell - I've tried it both ways ... with and without springboottest – tsar2512 Nov 18 '20 at 15:36
  • Oh; you are using `SpringApplicationBuilder`; sorry, missed that. See [the answers to this question](https://stackoverflow.com/questions/29669393/override-default-spring-boot-application-properties-settings-in-junit-test). – Gary Russell Nov 18 '20 at 15:43

1 Answers1

1

I resolved this by doing the following:

SpringBootTest(properties = {"spring.cloud.stream.function.definition=reverse"})
@Import(TestChannelBinderConfiguration.class)
public class EchoTransformerTest {

  @Autowired private InputDestination input;

  @Autowired private OutputDestination output;

  @Test
  public void testTransformer() {
    this.input.send(new GenericMessage<byte[]>("hello".getBytes()));
    assertThat(output.receive().getPayload()).isEqualTo("olleh".getBytes());
  }
}

and adding an application.yml to the test/resources this ensures that we don't read the src/resources application properties.

Another way was to explicitly define @TestPropertySource(locations= "test.yml")

tsar2512
  • 2,826
  • 3
  • 33
  • 61