Merge pull request #14806 from balasr21/master

BAEL-5889: Added implementation for Spy and SpyBean difference
This commit is contained in:
Maiklins 2023-10-04 18:14:59 +02:00 committed by GitHub
commit 7c6b5ac3de
8 changed files with 206 additions and 0 deletions

View File

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

View File

@ -0,0 +1,18 @@
package com.baeldung.spytest;
import org.springframework.stereotype.Component;
@Component
public class NotificationService {
private ExternalAlertService externalAlertService;
public void notify(Order order) {
System.out.println(order);
}
public boolean raiseAlert(Order order) {
return externalAlertService.alert(order);
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.spytest;
import java.util.UUID;
public class Order {
private UUID id;
private String name;
private OrderType orderType;
private double orderQuantity;
private String address;
public Order(UUID id, String name, double orderQuantity, String address) {
this.id = id;
this.name = name;
this.orderQuantity = orderQuantity;
this.address = address;
}
public enum OrderType {
INDIVIDUAL, BULK;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public double getOrderQuantity() {
return orderQuantity;
}
public String getAddress() {
return address;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.spytest;
import java.util.HashMap;
import java.util.UUID;
import org.springframework.stereotype.Component;
@Component
public class OrderRepository {
public static final HashMap<UUID, Order> orders = new HashMap<>();
public Order save(Order order) {
UUID orderId = UUID.randomUUID();
order.setId(orderId);
orders.put(UUID.randomUUID(), order);
return order;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.spytest;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
public final OrderRepository orderRepository;
public final NotificationService notificationService;
public OrderService(OrderRepository orderRepository, NotificationService notificationService) {
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;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.spytest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpyTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpyTestApplication.class, args);
}
}

View File

@ -0,0 +1,35 @@
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;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
@SpringBootTest
class OrderServiceIntegrationTest {
@Autowired
OrderRepository orderRepository;
@SpyBean
NotificationService notificationService;
@SpyBean
OrderService orderService;
@Test
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());
verify(notificationService).notify(any(Order.class));
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.spytest;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import java.util.UUID;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
class OrderServiceUnitTest {
@Spy
OrderRepository orderRepository;
@Spy
NotificationService notificationService;
@InjectMocks
OrderService orderService;
@Test
void givenNotificationServiceIsUsingSpy_whenOrderServiceIsCalled_thenNotificationServiceSpyShouldBeInvoked() {
UUID orderId = UUID.randomUUID();
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());
verify(notificationService).notify(any(Order.class));
}
}