diff --git a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/ExternalAlertService.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/ExternalAlertService.java new file mode 100644 index 0000000000..fd321a0e68 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/ExternalAlertService.java @@ -0,0 +1,6 @@ +package com.baeldung.spytest; + +public interface ExternalAlertService { + public boolean alert(Order order); + +} diff --git a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/NotificationService.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/NotificationService.java index 521e3b076e..f0b2f92bee 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/NotificationService.java +++ b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/NotificationService.java @@ -4,8 +4,15 @@ import org.springframework.stereotype.Component; @Component public class NotificationService { - public void notify(Order order){ + + private ExternalAlertService externalAlertService; + + public void notify(Order order) { System.out.println(order); } + public boolean raiseAlert(Order order) { + return externalAlertService.alert(order); + } + } diff --git a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/Order.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/Order.java index b7d7fad9e3..23f5a05e9d 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/Order.java +++ b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/Order.java @@ -8,6 +8,8 @@ public class Order { private String name; + private OrderType orderType; + private double orderQuantity; private String address; @@ -19,6 +21,10 @@ public class Order { this.address = address; } + public enum OrderType { + INDIVIDUAL, BULK; + } + public UUID getId() { return id; } @@ -39,3 +45,5 @@ public class Order { return address; } } + + diff --git a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/OrderRepository.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/OrderRepository.java index 986f7abde2..90fe5ba2b8 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/OrderRepository.java +++ b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/OrderRepository.java @@ -8,7 +8,7 @@ import org.springframework.stereotype.Component; @Component public class OrderRepository { - public static final HashMap orders=new HashMap<>(); + public static final HashMap orders = new HashMap<>(); public Order save(Order order) { UUID orderId = UUID.randomUUID(); diff --git a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/OrderService.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/OrderService.java index d08c96f5ae..47647b8721 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/OrderService.java +++ b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/OrderService.java @@ -13,9 +13,13 @@ public class OrderService { this.orderRepository = orderRepository; this.notificationService = notificationService; } + public Order save(Order order) { order = orderRepository.save(order); notificationService.notify(order); + if (!notificationService.raiseAlert(order)) { + throw new RuntimeException("Alert not raised"); + } return order; } } diff --git a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/SpyTestApplication.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/SpyTestApplication.java index bb2d3dc7d2..d8d81378df 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/SpyTestApplication.java +++ b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/spytest/SpyTestApplication.java @@ -2,6 +2,7 @@ package com.baeldung.spytest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; + @SpringBootApplication public class SpyTestApplication { diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/spytest/OrderServiceIntegrationTest.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/spytest/OrderServiceIntegrationTest.java index 22b0388b13..96c7b09dfc 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/spytest/OrderServiceIntegrationTest.java +++ b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/spytest/OrderServiceIntegrationTest.java @@ -1,6 +1,7 @@ package com.baeldung.spytest; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.verify; import org.junit.jupiter.api.Assertions; @@ -23,6 +24,8 @@ class OrderServiceIntegrationTest { void givenNotificationServiceIsUsingSpyBean_whenOrderServiceIsCalled_thenNotificationServiceSpyBeanShouldBeInvoked() { Order orderInput = new Order(null, "Test", 1.0, "17 St Andrews Croft, Leeds ,LS17 7TP"); + doReturn(true).when(notificationService) + .raiseAlert(any(Order.class)); Order order = orderService.save(orderInput); Assertions.assertNotNull(order); Assertions.assertNotNull(order.getId()); diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/spytest/OrderServiceUnitTest.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/spytest/OrderServiceUnitTest.java index b8f18c804c..21ae849bcf 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/spytest/OrderServiceUnitTest.java +++ b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/spytest/OrderServiceUnitTest.java @@ -30,6 +30,8 @@ class OrderServiceUnitTest { Order orderInput = new Order(orderId, "Test", 1.0, "17 St Andrews Croft, Leeds ,LS17 7TP"); doReturn(orderInput).when(orderRepository) .save(any()); + doReturn(true).when(notificationService) + .raiseAlert(any(Order.class)); Order order = orderService.save(orderInput); Assertions.assertNotNull(order); Assertions.assertEquals(orderId, order.getId());