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:
parent
14f336cf1a
commit
f324a50165
@ -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
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
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) {
|
||||
UUID orderId = UUID.randomUUID();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.baeldung.spytest;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpyTestApplication {
|
||||
|
||||
|
@ -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());
|
||||
|
@ -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());
|
||||
|
Loading…
x
Reference in New Issue
Block a user