这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
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
1 change: 1 addition & 0 deletions spring-boot-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<module>spring-boot-brave</module>
<module>spring-boot-simple</module>
<module>spring-boot-http2</module>
<module>spring-boot-testing-5</module>
</modules>

<build>
Expand Down
33 changes: 33 additions & 0 deletions spring-boot-modules/spring-boot-testing-5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>spring-boot-testing-5</artifactId>

<properties>
<java.version>21</java.version>
<junit-jupiter.version>5.12.2</junit-jupiter.version>
<logback.version>1.5.20</logback.version>
<spring-boot.version>3.5.7</spring-boot.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.baeldung.mockitospytest;

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.mockitospytest;

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.mockitospytest;

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.mockitospytest;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.UUID;

@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.mockitospytest;

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.mockitospytest;

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.mockitospytest;

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.test.context.bean.override.mockito.MockitoSpyBean;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

@SpringBootTest
class OrderServiceIntegrationTest {

@Autowired
OrderRepository orderRepository;
@MockitoSpyBean
NotificationService notificationService;
@MockitoSpyBean
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));
}

}