这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.baeldung.spytest;

public interface ExternalAlertService {
public boolean alert(Order order);

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}


Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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));
}

}