BAEL-5889: Updated with review comment to add another method inside @Spy which is served through a mock to demonstrate partial mock condition

This commit is contained in:
balasr3 2023-09-23 16:11:32 +05:30
parent 14f336cf1a
commit f324a50165
8 changed files with 33 additions and 2 deletions

@ -0,0 +1,6 @@
package com.baeldung.spytest;
public interface ExternalAlertService {
public boolean alert(Order order);
}

@ -4,8 +4,15 @@ import org.springframework.stereotype.Component;
@Component @Component
public class NotificationService { public class NotificationService {
public void notify(Order order){
private ExternalAlertService externalAlertService;
public void notify(Order order) {
System.out.println(order); System.out.println(order);
} }
public boolean raiseAlert(Order order) {
return externalAlertService.alert(order);
}
} }

@ -8,6 +8,8 @@ public class Order {
private String name; private String name;
private OrderType orderType;
private double orderQuantity; private double orderQuantity;
private String address; private String address;
@ -19,6 +21,10 @@ public class Order {
this.address = address; this.address = address;
} }
public enum OrderType {
INDIVIDUAL, BULK;
}
public UUID getId() { public UUID getId() {
return id; return id;
} }
@ -39,3 +45,5 @@ public class Order {
return address; return address;
} }
} }

@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
@Component @Component
public class OrderRepository { public class OrderRepository {
public static final HashMap<UUID, Order> orders=new HashMap<>(); public static final HashMap<UUID, Order> orders = new HashMap<>();
public Order save(Order order) { public Order save(Order order) {
UUID orderId = UUID.randomUUID(); UUID orderId = UUID.randomUUID();

@ -13,9 +13,13 @@ public class OrderService {
this.orderRepository = orderRepository; this.orderRepository = orderRepository;
this.notificationService = notificationService; this.notificationService = notificationService;
} }
public Order save(Order order) { public Order save(Order order) {
order = orderRepository.save(order); order = orderRepository.save(order);
notificationService.notify(order); notificationService.notify(order);
if (!notificationService.raiseAlert(order)) {
throw new RuntimeException("Alert not raised");
}
return order; return order;
} }
} }

@ -2,6 +2,7 @@ package com.baeldung.spytest;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class SpyTestApplication { public class SpyTestApplication {

@ -1,6 +1,7 @@
package com.baeldung.spytest; package com.baeldung.spytest;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
@ -23,6 +24,8 @@ class OrderServiceIntegrationTest {
void givenNotificationServiceIsUsingSpyBean_whenOrderServiceIsCalled_thenNotificationServiceSpyBeanShouldBeInvoked() { void givenNotificationServiceIsUsingSpyBean_whenOrderServiceIsCalled_thenNotificationServiceSpyBeanShouldBeInvoked() {
Order orderInput = new Order(null, "Test", 1.0, "17 St Andrews Croft, Leeds ,LS17 7TP"); 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); Order order = orderService.save(orderInput);
Assertions.assertNotNull(order); Assertions.assertNotNull(order);
Assertions.assertNotNull(order.getId()); Assertions.assertNotNull(order.getId());

@ -30,6 +30,8 @@ class OrderServiceUnitTest {
Order orderInput = new Order(orderId, "Test", 1.0, "17 St Andrews Croft, Leeds ,LS17 7TP"); Order orderInput = new Order(orderId, "Test", 1.0, "17 St Andrews Croft, Leeds ,LS17 7TP");
doReturn(orderInput).when(orderRepository) doReturn(orderInput).when(orderRepository)
.save(any()); .save(any());
doReturn(true).when(notificationService)
.raiseAlert(any(Order.class));
Order order = orderService.save(orderInput); Order order = orderService.save(orderInput);
Assertions.assertNotNull(order); Assertions.assertNotNull(order);
Assertions.assertEquals(orderId, order.getId()); Assertions.assertEquals(orderId, order.getId());