4

I am learning ho to test Spring Boot Kafka application with TestContainers. The test passes. However, in the beginning there are a lot of such messages:

2021-04-05 09:00:13.927  WARN 1864 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.

Later, producer connects to valid bootstrap servers:

    bootstrap.servers = [PLAINTEXT://localhost:55015]

How could I avoid such errors? They increase test time.

Here is the code: https://github.com/aleksei17/springboot-kafka/blob/master/src/test/java/com/example/kafka/springbootkafka/TestContainersTest1.java

Aleksei
  • 137
  • 1
  • 12

1 Answers1

2

I had the same problem, solved creating and application-test.yml with:

spring:
  kafka:
    bootstrap-servers: fake:1234

KafkaServerTestProvider.java:

@ActiveProfiles("test")
@Testcontainers
@Slf4j
public class KafkaServerTestProvider {
  public static final KafkaContainer KAFKA_CONTAINER =
      new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"));

  public static class KafkaServerInitializer
      implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(final ConfigurableApplicationContext applicationContext) {
      KAFKA_CONTAINER.start();
      TestPropertyValues.of(
              "spring.kafka.bootstrap-servers=" + KAFKA_CONTAINER.getBootstrapServers())
          .applyTo(applicationContext.getEnvironment());

      log.info("Kafka for testing: {}", KAFKA_CONTAINER.getBootstrapServers());
    }
  }
}

and the test:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
@ContextConfiguration(
    initializers = {KafkaServerTestProvider.KafkaServerInitializer.class},
    classes = Application.class)
class SampleServiceTestIT {
  @Autowired SampleService sampleService;

  @Test
  void sendMessageTest() {
    sampleService.sendMessage(
        "sampletopic",
        SampleRequest.builder().email("user@email.com").name("name").surname("surname").build());
  }
}
lzafra
  • 91
  • 2
  • Thanks! Adding this line to initializer solved the problem: `"spring.kafka.bootstrap-servers=" + kafka.getBootstrapServers()` – Aleksei Apr 13 '21 at 17:42
  • by doing some tests I have found a strange behavior, solved with: TestPropertyValues.of( "spring.kafka.config-servers=" + KAFKA_CONTAINER.getBootstrapServers(), "spring.kafka.consumer.bootstrap-servers=" + KAFKA_CONTAINER.getBootstrapServers(), "spring.kafka.producer.bootstrap-servers=" + KAFKA_CONTAINER.getBootstrapServers()) .applyTo(applicationContext.getEnvironment()); – lzafra Apr 14 '21 at 13:36
  • Are you sure there is a property named `spring.kafka.config-servers`? I have only seen `spring.kafka.bootstrap-servers`. – Aleksei Apr 15 '21 at 19:34
  • Or is it a custom property in your application? – Aleksei Apr 16 '21 at 06:05
  • true, I had a typo there, its spring.kafka.bootstrap-servers... thanks! – lzafra Apr 19 '21 at 11:39