0

I'm trying to test listener in springboot created using @KafkaListener But listener always listens on localhost:9092 instead of using this embededKafka

My listener looks like this:


@Component
@Slf4j
class SomeListener {
    private final List<String> receivedMessages = new ArrayList<>();

    @KafkaListener(topics = "some-ultra-cool-topic")
    public void onKafkaMessage(String theMessage) {
        log.info("Message received {}", theMessage);
        receivedMessages.add(theMessage);
    }

    Collection<String> getAll() {
        return unmodifiableCollection(receivedMessages);
    }
}

And spock test like this:

@SpringBootTest
@EmbeddedKafka
@TestPropertySource(properties = ['spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}', 'spring.kafka.consumer.auto-offset-reset=earliest'])
class SomeListenerTest extends Specification {
    @Autowired
    EmbeddedKafkaBroker embeddedKafkaBroker

    @Autowired
    SomeListener someListener

    void 'should receive message'() {
        given:
            def sender = new KafkaTemplate<>(new DefaultKafkaProducerFactory<String, String>(KafkaTestUtils.producerProps(embeddedKafkaBroker)))

        when:
            sender.send('some-ultra-cool-topic', 'first message content')
        then:
            someListener.all.size() == 1
    }

}

My application.yaml doesn't have bootstraps servers configured - so it is purly default from spring-boot.

I can see in logs that producer is sending message to broker (it starts every time on different random port). But listener always try to connect to broker on localhost:9092

How can I configure it to use this embedded one?

Emiter
  • 268
  • 2
  • 13
  • It's possible you have some race condition. Try adding Thread.sleep(5000) before ` someListener.all.size() == 1` – sawim Aug 24 '20 at 07:43
  • I added sleep as You suggested, but of course it doesn't change anything. In summary: 1. Producer is on random port. 2. Listener is always on localhost:9092 It doesn't matter how many sleeps I add. – Emiter Aug 24 '20 at 08:08
  • Ok, I found that after adding more sleep it works. So how can I do it without sleep ? Is there any KafkaUtils.waitForCursorMove() or sth? Still don't know why in logs it says it is using port :9092 – Emiter Aug 24 '20 at 09:28
  • Strange it started working. Anyway, I checked some of my projects, and there default broker address is set in main application.yaml and in test its overridden like in your example. Without setting default broker address integration test does not work, because it connects to localhost broker – sawim Aug 24 '20 at 09:51

1 Answers1

0

Thanks @sawim for tips

Actual problem was in test. I ended up doing this test with lib org.awaitility:awaitility

        then:
        waitAtMost(5, SECONDS)
                .untilAsserted({ ->
                    assertThat(personFacade.findAll(), hasSize(1))
                })

Configuration from first example works, however during startup I can see kafka-logs trying to connect to localhost:9200 - seems we can Ignore it

Emiter
  • 268
  • 2
  • 13