BAEL-7547: finishing spring modulith test

This commit is contained in:
emanuel.trandafir 2024-03-22 15:22:00 +01:00
parent 49e66a1a1b
commit 34fd8269e3
4 changed files with 38 additions and 25 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -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));
}
}