From 4a95314449f2e783c30df8cfd4999f772fea1811 Mon Sep 17 00:00:00 2001 From: "emanuel.trandafir" Date: Thu, 21 Mar 2024 18:07:54 +0100 Subject: [PATCH 1/8] BAEL-7547: testing spring events --- .../application/events/orders/Order.java | 12 ++++++ .../events/orders/OrderCompletedEvent.java | 6 +++ .../events/orders/OrderRepository.java | 25 +++++++++++ .../events/orders/OrderService.java | 29 +++++++++++++ .../rewards/LoyalCustomersRepository.java | 41 +++++++++++++++++++ .../events/rewards/LoyaltyPointsService.java | 28 +++++++++++++ .../events/ApplicationEventsTest.java | 38 +++++++++++++++++ 7 files changed, 179 insertions(+) create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/Order.java create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderCompletedEvent.java create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderRepository.java create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderService.java create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsTest.java diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/Order.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/Order.java new file mode 100644 index 0000000000..a0ba6815bc --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/Order.java @@ -0,0 +1,12 @@ +package com.baeldung.springmodulith.application.events.orders; + +import java.time.Instant; +import java.util.List; + +record Order(String id, String customerId, List productIds, Instant timestamp) { + + public Order(String customerId, List productIds) { + this(null, customerId, productIds, Instant.now()); + } + +} diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderCompletedEvent.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderCompletedEvent.java new file mode 100644 index 0000000000..0f11468241 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderCompletedEvent.java @@ -0,0 +1,6 @@ +package com.baeldung.springmodulith.application.events.orders; + +import java.time.Instant; + +public record OrderCompletedEvent(String orderId, String customerId, Instant time) { +} diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderRepository.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderRepository.java new file mode 100644 index 0000000000..d02b00d2d0 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderRepository.java @@ -0,0 +1,25 @@ +package com.baeldung.springmodulith.application.events.orders; + +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@Component +class OrderRepository { + private final List orders = new ArrayList<>(); + + public Order save(Order order) { + order = new Order(UUID.randomUUID().toString(), order.customerId(), order.productIds(), order.timestamp()); + orders.add(order); + return order; + } + + public List ordersByCustomer(String customerId) { + return orders.stream() + .filter(it -> it.customerId().equals(customerId)) + .toList(); + } + +} diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderService.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderService.java new file mode 100644 index 0000000000..c3b52d478b --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderService.java @@ -0,0 +1,29 @@ +package com.baeldung.springmodulith.application.events.orders; + +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Service; + +import java.util.Arrays; + +@Service +public class OrderService { + + private final OrderRepository repository; + private final ApplicationEventPublisher eventPublisher; + + public OrderService(OrderRepository orders, ApplicationEventPublisher eventsPublisher) { + this.repository = orders; + this.eventPublisher = eventsPublisher; + } + + public void placeOrder(String customerId, String... productIds) { + Order order = new Order(customerId, Arrays.asList(productIds)); + // business logic to validate and place the order + + Order savedOrder = repository.save(order); + + OrderCompletedEvent event = new OrderCompletedEvent(savedOrder.id(), savedOrder.customerId(), savedOrder.timestamp()); + eventPublisher.publishEvent(event); + } + +} diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java new file mode 100644 index 0000000000..764924232d --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java @@ -0,0 +1,41 @@ +package com.baeldung.springmodulith.application.events.rewards; + +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Component +public class LoyalCustomersRepository { + + private List customers = new ArrayList<>(); + + public Optional find(String customerId) { + return customers.stream() + .filter(it -> it.customerId().equals(customerId)) + .findFirst(); + } + + public void awardPoints(String customerId, int points) { + var customer = find(customerId).orElseThrow(); + customers.remove(customer); + customers.add(customer.addPoints(points)); + } + + public void save(String customerId) { + customers.add(new LoyalCustomer(customerId, 0)); + } + + public boolean isLoyalCustomer(String customerId) { + return find(customerId).isPresent(); + } + + public record LoyalCustomer(String customerId, int points) { + + LoyalCustomer addPoints(int points) { + return new LoyalCustomer(customerId, this.points() + points); + } + } + +} diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java new file mode 100644 index 0000000000..20af0caa6f --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java @@ -0,0 +1,28 @@ +package com.baeldung.springmodulith.application.events.rewards; + +import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Service; + +@Service +public class LoyaltyPointsService { + + public static final int ORDER_COMPLETED_POINTS = 10; + public static final int SING_UP_POINTS = 50; + private final LoyalCustomersRepository loyalCustomers; + + public LoyaltyPointsService(LoyalCustomersRepository loyalCustomers) { + this.loyalCustomers = loyalCustomers; + } + + @EventListener + public void onOrderCompleted(OrderCompletedEvent event) { + if (loyalCustomers.find(event.customerId()).isEmpty()) { + loyalCustomers.save(event.customerId()); + loyalCustomers.awardPoints(event.customerId(), SING_UP_POINTS); + } + + loyalCustomers.awardPoints(event.customerId(), ORDER_COMPLETED_POINTS); + } + +} diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsTest.java new file mode 100644 index 0000000000..82fd53d13e --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsTest.java @@ -0,0 +1,38 @@ +package com.baeldung.springmodulith.application.events; + +import com.baeldung.springmodulith.application.events.orders.OrderService; +import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository; +import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository.LoyalCustomer; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; + +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") +public class ApplicationEventsTest { + + + @Autowired + OrderService orderService; + + @Autowired + LoyalCustomersRepository loyalCustomers; + + @Test + void whenNewCustomerCompletesAnOrder_thenHeReceivesSingUpPointsPlusOrderPoints() { + orderService.placeOrder("customer1", "product1", "product2"); + + Optional customer = loyalCustomers.find("customer1"); + + assertThat(customer).isPresent() + .get() + .extracting(LoyalCustomer::points) + .isEqualTo(50 + 10); + } + +} From 5e9a5b9b3f19590bcf6698227c4df735414e5c97 Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Thu, 21 Mar 2024 23:26:12 +0100 Subject: [PATCH 2/8] BAEL-7574: added tests --- .../events/orders/OrderCompletedEvent.java | 2 +- ...st.java => ApplicationEventsUnitTest.java} | 3 +- .../events/EventListenerUnitTest.java | 39 +++++++++++++++++++ .../events/EventPublisherUnitTest.java | 39 +++++++++++++++++++ .../application/events/TestEventListener.java | 29 ++++++++++++++ 5 files changed, 109 insertions(+), 3 deletions(-) rename spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/{ApplicationEventsTest.java => ApplicationEventsUnitTest.java} (96%) create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/TestEventListener.java diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderCompletedEvent.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderCompletedEvent.java index 0f11468241..4344b336ac 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderCompletedEvent.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderCompletedEvent.java @@ -2,5 +2,5 @@ package com.baeldung.springmodulith.application.events.orders; import java.time.Instant; -public record OrderCompletedEvent(String orderId, String customerId, Instant time) { +public record OrderCompletedEvent(String orderId, String customerId, Instant timestamp) { } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsUnitTest.java similarity index 96% rename from spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsTest.java rename to spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsUnitTest.java index 82fd53d13e..0ebf96b33f 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsUnitTest.java @@ -14,8 +14,7 @@ import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest @ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") -public class ApplicationEventsTest { - +public class ApplicationEventsUnitTest { @Autowired OrderService orderService; diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java new file mode 100644 index 0000000000..b8ad79e6f1 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.springmodulith.application.events; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Instant; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.annotation.ComponentScan; + +import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; +import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository; + +@SpringBootTest +@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") +public class EventListenerUnitTest { + + @Autowired + private LoyalCustomersRepository customers; + + @Autowired + private ApplicationEventPublisher testEventPublisher; + + @Test + void whenPublishingOrderCompletedEvent_thenRewardCustomerWithLoyaltyPoints() { + OrderCompletedEvent event = new OrderCompletedEvent("order-1", "customer-1", Instant.now()); + + testEventPublisher.publishEvent(event); + + assertThat(customers.find("customer-1")) + .isPresent().get() + .hasFieldOrPropertyWithValue("customerId", "customer-1") + .hasFieldOrPropertyWithValue("points", 60); + } + +} diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java new file mode 100644 index 0000000000..0989bb5fd3 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.springmodulith.application.events; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; + +import com.baeldung.springmodulith.application.events.orders.OrderService; + +@SpringBootTest +@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") +public class EventPublisherUnitTest { + + @Autowired + OrderService orderService; + + @Autowired + TestEventListener testEventListener; + + @BeforeEach + void beforeEach() { + testEventListener.reset(); + } + + @Test + void whenPlacingOrder_thenPublishApplicationEvent() { + orderService.placeOrder("customer1", "product1", "product2"); + + assertThat(testEventListener.getEvents()) + .hasSize(1).first() + .hasFieldOrPropertyWithValue("customerId", "customer1") + .hasFieldOrProperty("orderId") + .hasFieldOrProperty("timestamp"); + } + +} diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/TestEventListener.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/TestEventListener.java new file mode 100644 index 0000000000..8973a99355 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/TestEventListener.java @@ -0,0 +1,29 @@ +package com.baeldung.springmodulith.application.events; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; + +@Component +class TestEventListener { + + private final List events = new ArrayList<>(); + + @EventListener + void onEvent(OrderCompletedEvent event) { + events.add(event); + } + + public List getEvents() { + return events; + } + + public void reset() { + events.clear(); + } +} + From 49e66a1a1b8f1d6617e6b518e40e95be89ae1193 Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Thu, 21 Mar 2024 23:40:12 +0100 Subject: [PATCH 3/8] BAEL-5747: added spring modulith test --- .../spring-boot-libraries-3/pom.xml | 6 ++++++ ...=> SpringModulithScenarioApiUnitTest.java} | 20 ++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) rename spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/{ApplicationEventsUnitTest.java => SpringModulithScenarioApiUnitTest.java} (52%) diff --git a/spring-boot-modules/spring-boot-libraries-3/pom.xml b/spring-boot-modules/spring-boot-libraries-3/pom.xml index 988ce0bafe..7766af84c8 100644 --- a/spring-boot-modules/spring-boot-libraries-3/pom.xml +++ b/spring-boot-modules/spring-boot-libraries-3/pom.xml @@ -55,6 +55,12 @@ test + + org.springframework.modulith + spring-modulith-starter-test + 1.1.3 + + org.testcontainers kafka diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java similarity index 52% rename from spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsUnitTest.java rename to spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java index 0ebf96b33f..17fe4a555c 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/ApplicationEventsUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java @@ -2,19 +2,18 @@ package com.baeldung.springmodulith.application.events; import com.baeldung.springmodulith.application.events.orders.OrderService; import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository; -import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository.LoyalCustomer; + import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.ComponentScan; - -import java.util.Optional; +import org.springframework.modulith.test.ApplicationModuleTest; +import org.springframework.modulith.test.Scenario; import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest +@ApplicationModuleTest @ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") -public class ApplicationEventsUnitTest { +public class SpringModulithScenarioApiUnitTest { @Autowired OrderService orderService; @@ -23,15 +22,8 @@ public class ApplicationEventsUnitTest { LoyalCustomersRepository loyalCustomers; @Test - void whenNewCustomerCompletesAnOrder_thenHeReceivesSingUpPointsPlusOrderPoints() { - orderService.placeOrder("customer1", "product1", "product2"); + void test(Scenario scenario) { - Optional customer = loyalCustomers.find("customer1"); - - assertThat(customer).isPresent() - .get() - .extracting(LoyalCustomer::points) - .isEqualTo(50 + 10); } } From 34fd8269e3848908615afbc7aa1712d7417a1f9a Mon Sep 17 00:00:00 2001 From: "emanuel.trandafir" Date: Fri, 22 Mar 2024 15:22:00 +0100 Subject: [PATCH 4/8] BAEL-7547: finishing spring modulith test --- .../events/rewards/LoyaltyPointsService.java | 9 ++---- .../events/EventListenerUnitTest.java | 8 ++--- .../events/EventPublisherUnitTest.java | 15 +++++---- .../SpringModulithScenarioApiUnitTest.java | 31 +++++++++++++++---- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java index 20af0caa6f..3febcf0d66 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java @@ -7,8 +7,7 @@ import org.springframework.stereotype.Service; @Service public class LoyaltyPointsService { - public static final int ORDER_COMPLETED_POINTS = 10; - public static final int SING_UP_POINTS = 50; + public static final int ORDER_COMPLETED_POINTS = 60; private final LoyalCustomersRepository loyalCustomers; public LoyaltyPointsService(LoyalCustomersRepository loyalCustomers) { @@ -17,11 +16,7 @@ public class LoyaltyPointsService { @EventListener public void onOrderCompleted(OrderCompletedEvent event) { - if (loyalCustomers.find(event.customerId()).isEmpty()) { - loyalCustomers.save(event.customerId()); - loyalCustomers.awardPoints(event.customerId(), SING_UP_POINTS); - } - + // business logic to award points to loyal customers loyalCustomers.awardPoints(event.customerId(), ORDER_COMPLETED_POINTS); } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java index b8ad79e6f1..104cc1690d 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java @@ -16,7 +16,7 @@ import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepo @SpringBootTest @ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") -public class EventListenerUnitTest { +class EventListenerUnitTest { @Autowired private LoyalCustomersRepository customers; @@ -31,9 +31,9 @@ public class EventListenerUnitTest { testEventPublisher.publishEvent(event); assertThat(customers.find("customer-1")) - .isPresent().get() - .hasFieldOrPropertyWithValue("customerId", "customer-1") - .hasFieldOrPropertyWithValue("points", 60); + .isPresent().get() + .hasFieldOrPropertyWithValue("customerId", "customer-1") + .hasFieldOrPropertyWithValue("points", 60); } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java index 0989bb5fd3..16f20defc3 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java @@ -1,18 +1,17 @@ package com.baeldung.springmodulith.application.events; -import static org.assertj.core.api.Assertions.assertThat; - +import com.baeldung.springmodulith.application.events.orders.OrderService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.ComponentScan; -import com.baeldung.springmodulith.application.events.orders.OrderService; +import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest @ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") -public class EventPublisherUnitTest { +class EventPublisherUnitTest { @Autowired OrderService orderService; @@ -30,10 +29,10 @@ public class EventPublisherUnitTest { orderService.placeOrder("customer1", "product1", "product2"); assertThat(testEventListener.getEvents()) - .hasSize(1).first() - .hasFieldOrPropertyWithValue("customerId", "customer1") - .hasFieldOrProperty("orderId") - .hasFieldOrProperty("timestamp"); + .hasSize(1).first() + .hasFieldOrPropertyWithValue("customerId", "customer1") + .hasFieldOrProperty("orderId") + .hasFieldOrProperty("timestamp"); } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java index 17fe4a555c..8d262a24d0 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java @@ -1,19 +1,22 @@ package com.baeldung.springmodulith.application.events; +import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; import com.baeldung.springmodulith.application.events.orders.OrderService; import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository; - import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.ComponentScan; import org.springframework.modulith.test.ApplicationModuleTest; +import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode; import org.springframework.modulith.test.Scenario; +import java.time.Duration; +import java.time.Instant; + +import static java.time.Duration.ofMillis; import static org.assertj.core.api.Assertions.assertThat; @ApplicationModuleTest -@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") -public class SpringModulithScenarioApiUnitTest { +class SpringModulithScenarioApiUnitTest { @Autowired OrderService orderService; @@ -22,8 +25,24 @@ public class SpringModulithScenarioApiUnitTest { LoyalCustomersRepository loyalCustomers; @Test - void test(Scenario scenario) { - + void whenPlacingOrder_thenPublishOrderCompletedEvent(Scenario scenario) { + scenario.stimulate(() -> orderService.placeOrder("customer-1", "product-1", "product-2")) + .andWaitAtMost(ofMillis(500)) + .andWaitForEventOfType(OrderCompletedEvent.class) + .toArriveAndVerify(evt -> assertThat(evt) + .hasFieldOrPropertyWithValue("customerId", "customer-1") + .hasFieldOrProperty("orderId") + .hasFieldOrProperty("timestamp")); } + @Test + void whenReceivingPublishOrderCompletedEvent_thenRewardCustomerWithLoyaltyPoints(Scenario scenario) { + scenario.publish(new OrderCompletedEvent("order-1", "customer-1", Instant.now())) + .andWaitAtMost(ofMillis(500)) + .andWaitForStateChange(() -> loyalCustomers.find("customer-1")) + .andVerify(it -> assertThat(it) + .isPresent().get() + .hasFieldOrPropertyWithValue("customerId", "customer-1") + .hasFieldOrPropertyWithValue("points", 60)); + } } From 23ff3bc2fc1ffa701f17f9daa312cf6df7ab065a Mon Sep 17 00:00:00 2001 From: "emanuel.trandafir" Date: Fri, 22 Mar 2024 15:37:18 +0100 Subject: [PATCH 5/8] BAEL-7547: small fix --- .../events/rewards/LoyalCustomersRepository.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java index 764924232d..c4afae0d5d 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java @@ -13,18 +13,21 @@ public class LoyalCustomersRepository { public Optional find(String customerId) { return customers.stream() - .filter(it -> it.customerId().equals(customerId)) - .findFirst(); + .filter(it -> it.customerId().equals(customerId)) + .findFirst(); } public void awardPoints(String customerId, int points) { - var customer = find(customerId).orElseThrow(); + var customer = find(customerId) + .orElseGet(() -> save(new LoyalCustomer(customerId, 0))); + customers.remove(customer); customers.add(customer.addPoints(points)); } - public void save(String customerId) { - customers.add(new LoyalCustomer(customerId, 0)); + public LoyalCustomer save(LoyalCustomer customer) { + customers.add(customer); + return customer; } public boolean isLoyalCustomer(String customerId) { From fbd72d1a0967716ccf79d68ea05d0512b7d684bd Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Fri, 22 Mar 2024 22:11:09 +0100 Subject: [PATCH 6/8] BAEL-7547: removing timeouts --- .../application/events/SpringModulithScenarioApiUnitTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java index 8d262a24d0..f36a0c30e6 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java @@ -27,7 +27,6 @@ class SpringModulithScenarioApiUnitTest { @Test void whenPlacingOrder_thenPublishOrderCompletedEvent(Scenario scenario) { scenario.stimulate(() -> orderService.placeOrder("customer-1", "product-1", "product-2")) - .andWaitAtMost(ofMillis(500)) .andWaitForEventOfType(OrderCompletedEvent.class) .toArriveAndVerify(evt -> assertThat(evt) .hasFieldOrPropertyWithValue("customerId", "customer-1") @@ -38,7 +37,6 @@ class SpringModulithScenarioApiUnitTest { @Test void whenReceivingPublishOrderCompletedEvent_thenRewardCustomerWithLoyaltyPoints(Scenario scenario) { scenario.publish(new OrderCompletedEvent("order-1", "customer-1", Instant.now())) - .andWaitAtMost(ofMillis(500)) .andWaitForStateChange(() -> loyalCustomers.find("customer-1")) .andVerify(it -> assertThat(it) .isPresent().get() From 9502473ac259620ddbb7869aa85ec306645e8999 Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Fri, 22 Mar 2024 22:30:39 +0100 Subject: [PATCH 7/8] BAEL-7547: adding h2 for non-testcontainers tests --- spring-boot-modules/spring-boot-libraries-3/pom.xml | 7 +++++++ .../application/events/EventListenerUnitTest.java | 1 - .../application/events/EventPublisherUnitTest.java | 1 - 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries-3/pom.xml b/spring-boot-modules/spring-boot-libraries-3/pom.xml index 7766af84c8..39d594cdfe 100644 --- a/spring-boot-modules/spring-boot-libraries-3/pom.xml +++ b/spring-boot-modules/spring-boot-libraries-3/pom.xml @@ -61,6 +61,13 @@ 1.1.3 + + com.h2database + h2 + 2.2.224 + test + + org.testcontainers kafka diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java index 104cc1690d..a54d003eb2 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java @@ -15,7 +15,6 @@ import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository; @SpringBootTest -@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") class EventListenerUnitTest { @Autowired diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java index 16f20defc3..f4bdeee90d 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java @@ -10,7 +10,6 @@ import org.springframework.context.annotation.ComponentScan; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest -@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") class EventPublisherUnitTest { @Autowired From 7d14d37fda9fa878be3a6855751d5d20fd78a460 Mon Sep 17 00:00:00 2001 From: "emanuel.trandafir" Date: Thu, 28 Mar 2024 10:49:04 +0100 Subject: [PATCH 8/8] BAEL-7547: formatting & code review --- .../spring-boot-libraries-3/pom.xml | 38 +++++++------- .../application/events/orders/Order.java | 6 +-- .../events/orders/OrderRepository.java | 28 +++++----- .../events/orders/OrderService.java | 30 +++++------ .../rewards/LoyalCustomersRepository.java | 52 +++++++++---------- .../events/rewards/LoyaltyPointsService.java | 23 ++++---- .../events/EventListenerUnitTest.java | 28 +++++----- 7 files changed, 102 insertions(+), 103 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries-3/pom.xml b/spring-boot-modules/spring-boot-libraries-3/pom.xml index 39d594cdfe..e88ae4c078 100644 --- a/spring-boot-modules/spring-boot-libraries-3/pom.xml +++ b/spring-boot-modules/spring-boot-libraries-3/pom.xml @@ -17,7 +17,6 @@ org.springframework.boot spring-boot-starter-data-jpa - org.springframework.kafka spring-kafka @@ -28,20 +27,27 @@ postgresql ${postgresql.version} + org.springframework.modulith spring-modulith-events-api - ${spring-modulith-events-kafka.version} + ${spring-modulith.version} org.springframework.modulith spring-modulith-events-kafka - ${spring-modulith-events-kafka.version} + ${spring-modulith.version} org.springframework.modulith spring-modulith-starter-jpa - ${spring-modulith-events-kafka.version} + ${spring-modulith.version} + + + org.springframework.modulith + spring-modulith-starter-test + ${spring-modulith.version} + test @@ -54,20 +60,6 @@ spring-boot-testcontainers test - - - org.springframework.modulith - spring-modulith-starter-test - 1.1.3 - - - - com.h2database - h2 - 2.2.224 - test - - org.testcontainers kafka @@ -86,7 +78,6 @@ ${testcontainers.version} test - org.testcontainers postgresql @@ -100,16 +91,23 @@ ${awaitility.version} test + + com.h2database + h2 + ${h2.version} + test + 17 3.1.5 - 1.1.2 + 1.1.3 1.19.3 4.2.0 42.3.1 + 2.2.224 diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/Order.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/Order.java index a0ba6815bc..c448bd44dd 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/Order.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/Order.java @@ -5,8 +5,8 @@ import java.util.List; record Order(String id, String customerId, List productIds, Instant timestamp) { - public Order(String customerId, List productIds) { - this(null, customerId, productIds, Instant.now()); - } + public Order(String customerId, List productIds) { + this(null, customerId, productIds, Instant.now()); + } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderRepository.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderRepository.java index d02b00d2d0..7c159e3582 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderRepository.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderRepository.java @@ -1,25 +1,27 @@ package com.baeldung.springmodulith.application.events.orders; -import org.springframework.stereotype.Component; - import java.util.ArrayList; import java.util.List; import java.util.UUID; +import org.springframework.stereotype.Component; + @Component class OrderRepository { - private final List orders = new ArrayList<>(); + private final List orders = new ArrayList<>(); - public Order save(Order order) { - order = new Order(UUID.randomUUID().toString(), order.customerId(), order.productIds(), order.timestamp()); - orders.add(order); - return order; - } + public Order save(Order order) { + order = new Order(UUID.randomUUID() + .toString(), order.customerId(), order.productIds(), order.timestamp()); + orders.add(order); + return order; + } - public List ordersByCustomer(String customerId) { - return orders.stream() - .filter(it -> it.customerId().equals(customerId)) - .toList(); - } + public List ordersByCustomer(String customerId) { + return orders.stream() + .filter(it -> it.customerId() + .equals(customerId)) + .toList(); + } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderService.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderService.java index c3b52d478b..c60792813c 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderService.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/orders/OrderService.java @@ -1,29 +1,29 @@ package com.baeldung.springmodulith.application.events.orders; +import java.util.Arrays; + import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; -import java.util.Arrays; - @Service public class OrderService { - private final OrderRepository repository; - private final ApplicationEventPublisher eventPublisher; + private final OrderRepository repository; + private final ApplicationEventPublisher eventPublisher; - public OrderService(OrderRepository orders, ApplicationEventPublisher eventsPublisher) { - this.repository = orders; - this.eventPublisher = eventsPublisher; - } + public OrderService(OrderRepository orders, ApplicationEventPublisher eventsPublisher) { + this.repository = orders; + this.eventPublisher = eventsPublisher; + } - public void placeOrder(String customerId, String... productIds) { - Order order = new Order(customerId, Arrays.asList(productIds)); - // business logic to validate and place the order + public void placeOrder(String customerId, String... productIds) { + Order order = new Order(customerId, Arrays.asList(productIds)); + // business logic to validate and place the order - Order savedOrder = repository.save(order); + Order savedOrder = repository.save(order); - OrderCompletedEvent event = new OrderCompletedEvent(savedOrder.id(), savedOrder.customerId(), savedOrder.timestamp()); - eventPublisher.publishEvent(event); - } + OrderCompletedEvent event = new OrderCompletedEvent(savedOrder.id(), savedOrder.customerId(), savedOrder.timestamp()); + eventPublisher.publishEvent(event); + } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java index c4afae0d5d..29ba6fa8e2 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java @@ -1,44 +1,44 @@ package com.baeldung.springmodulith.application.events.rewards; -import org.springframework.stereotype.Component; - import java.util.ArrayList; import java.util.List; import java.util.Optional; +import org.springframework.stereotype.Component; + @Component public class LoyalCustomersRepository { - private List customers = new ArrayList<>(); + private List customers = new ArrayList<>(); - public Optional find(String customerId) { - return customers.stream() - .filter(it -> it.customerId().equals(customerId)) - .findFirst(); - } + public Optional find(String customerId) { + return customers.stream() + .filter(it -> it.customerId() + .equals(customerId)) + .findFirst(); + } - public void awardPoints(String customerId, int points) { - var customer = find(customerId) - .orElseGet(() -> save(new LoyalCustomer(customerId, 0))); + public void awardPoints(String customerId, int points) { + var customer = find(customerId).orElseGet(() -> save(new LoyalCustomer(customerId, 0))); - customers.remove(customer); - customers.add(customer.addPoints(points)); - } + customers.remove(customer); + customers.add(customer.addPoints(points)); + } - public LoyalCustomer save(LoyalCustomer customer) { - customers.add(customer); - return customer; - } + public LoyalCustomer save(LoyalCustomer customer) { + customers.add(customer); + return customer; + } - public boolean isLoyalCustomer(String customerId) { - return find(customerId).isPresent(); - } + public boolean isLoyalCustomer(String customerId) { + return find(customerId).isPresent(); + } - public record LoyalCustomer(String customerId, int points) { + public record LoyalCustomer(String customerId, int points) { - LoyalCustomer addPoints(int points) { - return new LoyalCustomer(customerId, this.points() + points); - } - } + LoyalCustomer addPoints(int points) { + return new LoyalCustomer(customerId, this.points() + points); + } + } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java index 3febcf0d66..8cd1afe329 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java @@ -1,23 +1,24 @@ package com.baeldung.springmodulith.application.events.rewards; -import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; +import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; + @Service public class LoyaltyPointsService { - public static final int ORDER_COMPLETED_POINTS = 60; - private final LoyalCustomersRepository loyalCustomers; + public static final int ORDER_COMPLETED_POINTS = 60; + private final LoyalCustomersRepository loyalCustomers; - public LoyaltyPointsService(LoyalCustomersRepository loyalCustomers) { - this.loyalCustomers = loyalCustomers; - } + public LoyaltyPointsService(LoyalCustomersRepository loyalCustomers) { + this.loyalCustomers = loyalCustomers; + } - @EventListener - public void onOrderCompleted(OrderCompletedEvent event) { - // business logic to award points to loyal customers - loyalCustomers.awardPoints(event.customerId(), ORDER_COMPLETED_POINTS); - } + @EventListener + public void onOrderCompleted(OrderCompletedEvent event) { + // business logic to award points to loyal customers + loyalCustomers.awardPoints(event.customerId(), ORDER_COMPLETED_POINTS); + } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java index a54d003eb2..676bc1173b 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java @@ -4,12 +4,10 @@ import static org.assertj.core.api.Assertions.assertThat; import java.time.Instant; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.context.annotation.ComponentScan; import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository; @@ -17,22 +15,22 @@ import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepo @SpringBootTest class EventListenerUnitTest { - @Autowired - private LoyalCustomersRepository customers; + @Autowired + private LoyalCustomersRepository customers; - @Autowired - private ApplicationEventPublisher testEventPublisher; + @Autowired + private ApplicationEventPublisher testEventPublisher; - @Test - void whenPublishingOrderCompletedEvent_thenRewardCustomerWithLoyaltyPoints() { - OrderCompletedEvent event = new OrderCompletedEvent("order-1", "customer-1", Instant.now()); + @Test + void whenPublishingOrderCompletedEvent_thenRewardCustomerWithLoyaltyPoints() { + OrderCompletedEvent event = new OrderCompletedEvent("order-1", "customer-1", Instant.now()); - testEventPublisher.publishEvent(event); + testEventPublisher.publishEvent(event); - assertThat(customers.find("customer-1")) - .isPresent().get() - .hasFieldOrPropertyWithValue("customerId", "customer-1") - .hasFieldOrPropertyWithValue("points", 60); - } + assertThat(customers.find("customer-1")) + .isPresent().get() + .hasFieldOrPropertyWithValue("customerId", "customer-1") + .hasFieldOrPropertyWithValue("points", 60); + } }