BAEL-5889: Added code for Spy and SpyBean difference implementation

This commit is contained in:
balasr3 2023-09-21 23:59:34 +05:30
parent 306f1335b8
commit 88ea37c981
8 changed files with 176 additions and 0 deletions

View File

@ -0,0 +1,11 @@
package com.baeldung.spytest;
import org.springframework.stereotype.Component;
@Component
public class NotificationService {
public void notify(Order order){
System.out.println(order);
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.spytest;
import java.util.UUID;
public class Order {
private UUID id;
private String name;
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 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,21 @@
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);
return order;
}
}

View File

@ -0,0 +1,12 @@
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,32 @@
package com.baeldung.spytest;
import static org.mockito.ArgumentMatchers.any;
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");
Order order = orderService.save(orderInput);
Assertions.assertNotNull(order);
Assertions.assertNotNull(order.getId());
verify(notificationService).notify(any(Order.class));
}
}

View File

@ -0,0 +1,39 @@
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());
Order order = orderService.save(orderInput);
Assertions.assertNotNull(order);
Assertions.assertEquals(orderId, order.getId());
verify(notificationService).notify(any(Order.class));
}
}

View File

@ -1,6 +1,7 @@
package com.baeldung.prevent.commandline.application.runner.execution;
import org.junit.jupiter.api.Test;
import org.mockito.Spy;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;