Spring Boot is [...]
"); + + baeldung.createArticle(article); + + await().untilAsserted(() -> + assertThat(listener.getEvents()) + .hasSize(1) + .first().asString() + .contains("\"slug\":\"introduction-to-spring-boot\"") + .contains("\"title\":\"Introduction to Spring Boot\"")); + + assertThat(repository.findAll()) + .hasSize(1) + .first() + .extracting(Article::slug, Article::title) + .containsExactly("introduction-to-spring-boot", "Introduction to Spring Boot"); + } + + @Test + void whenPublishingMessageFails_thenArticleIsStillSavedToDB() { + var article = new Article(null, "Introduction to Spring Boot", "John Doe", "Spring Boot is [...]
"); + + baeldung.createArticle(article); + + assertThat(listener.getEvents()) + .isEmpty(); + + assertThat(repository.findAll()) + .hasSize(1) + .first() + .extracting(Article::title, Article::author) + .containsExactly("Introduction to Spring Boot", "John Doe"); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/events/externalization/listener/TestKafkaListenerConfig.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/events/externalization/listener/TestKafkaListenerConfig.java new file mode 100644 index 0000000000..c2ee9b24a2 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/events/externalization/listener/TestKafkaListenerConfig.java @@ -0,0 +1,31 @@ +package com.baeldung.springmodulith.events.externalization.listener; + +import org.springframework.boot.autoconfigure.kafka.KafkaProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.kafka.annotation.EnableKafka; +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; +import org.springframework.kafka.config.KafkaListenerContainerFactory; +import org.springframework.kafka.core.ConsumerFactory; +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; +import org.springframework.kafka.listener.ConcurrentMessageListenerContainer; + +@EnableKafka +@Configuration +public class TestKafkaListenerConfig { + + @Bean + KafkaListenerContainerFactory