Skip to content

Commit 3f96ca3

Browse files
committed
BAEL-9322 What is @MockitoSpyBean in Spring
1 parent 042c685 commit 3f96ca3

File tree

9 files changed

+194
-0
lines changed

9 files changed

+194
-0
lines changed

spring-boot-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<module>spring-boot-testing-2</module>
9595
<module>spring-boot-testing-3</module>
9696
<module>spring-boot-testing-4</module>
97+
<module>spring-boot-testing-5</module>
9798
<module>spring-boot-testing-spock</module>
9899
<module>spring-boot-vue</module>
99100
<module>spring-boot-actuator</module>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung.spring-boot-modules</groupId>
8+
<artifactId>spring-boot-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>spring-boot-testing-5</artifactId>
13+
14+
<properties>
15+
<java.version>21</java.version>
16+
<junit-jupiter.version>5.12.2</junit-jupiter.version>
17+
<logback.version>1.5.20</logback.version>
18+
<spring-boot.version>3.5.7</spring-boot.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-test</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
</project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.mockitospytest;
2+
3+
public interface ExternalAlertService {
4+
public boolean alert(Order order);
5+
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.mockitospytest;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class NotificationService {
7+
8+
private ExternalAlertService externalAlertService;
9+
10+
public void notify(Order order) {
11+
System.out.println(order);
12+
}
13+
14+
public boolean raiseAlert(Order order) {
15+
return externalAlertService.alert(order);
16+
}
17+
18+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.mockitospytest;
2+
3+
import java.util.UUID;
4+
5+
public class Order {
6+
7+
private UUID id;
8+
9+
private String name;
10+
11+
private OrderType orderType;
12+
13+
private double orderQuantity;
14+
15+
private String address;
16+
17+
public Order(UUID id, String name, double orderQuantity, String address) {
18+
this.id = id;
19+
this.name = name;
20+
this.orderQuantity = orderQuantity;
21+
this.address = address;
22+
}
23+
24+
public enum OrderType {
25+
INDIVIDUAL, BULK;
26+
}
27+
28+
public UUID getId() {
29+
return id;
30+
}
31+
32+
public void setId(UUID id) {
33+
this.id = id;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public double getOrderQuantity() {
41+
return orderQuantity;
42+
}
43+
44+
public String getAddress() {
45+
return address;
46+
}
47+
}
48+
49+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.mockitospytest;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import java.util.HashMap;
6+
import java.util.UUID;
7+
8+
@Component
9+
public class OrderRepository {
10+
11+
public static final HashMap<UUID, Order> orders = new HashMap<>();
12+
13+
public Order save(Order order) {
14+
UUID orderId = UUID.randomUUID();
15+
order.setId(orderId);
16+
orders.put(UUID.randomUUID(), order);
17+
return order;
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.mockitospytest;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
@Service
6+
public class OrderService {
7+
8+
public final OrderRepository orderRepository;
9+
10+
public final NotificationService notificationService;
11+
12+
public OrderService(OrderRepository orderRepository, NotificationService notificationService) {
13+
this.orderRepository = orderRepository;
14+
this.notificationService = notificationService;
15+
}
16+
17+
public Order save(Order order) {
18+
order = orderRepository.save(order);
19+
notificationService.notify(order);
20+
if (!notificationService.raiseAlert(order)) {
21+
throw new RuntimeException("Alert not raised");
22+
}
23+
return order;
24+
}
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.mockitospytest;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpyTestApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpyTestApplication.class, args);
11+
}
12+
13+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.mockitospytest;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
7+
8+
import static org.mockito.ArgumentMatchers.any;
9+
import static org.mockito.Mockito.doReturn;
10+
import static org.mockito.Mockito.verify;
11+
12+
@SpringBootTest
13+
class OrderServiceUnitTest {
14+
@MockitoSpyBean
15+
NotificationService notificationService;
16+
@MockitoSpyBean
17+
OrderService orderService;
18+
19+
@Test
20+
void givenNotificationServiceIsUsingSpyBean_whenOrderServiceIsCalled_thenNotificationServiceSpyBeanShouldBeInvoked() {
21+
Order orderInput = new Order(null, "Test", 1.0, "17 St Andrews Croft, Leeds ,LS17 7TP");
22+
doReturn(true).when(notificationService)
23+
.raiseAlert(any(Order.class));
24+
Order order = orderService.save(orderInput);
25+
Assertions.assertNotNull(order);
26+
Assertions.assertNotNull(order.getId());
27+
verify(notificationService).notify(any(Order.class));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)