Skip to content

Commit f974f74

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

File tree

9 files changed

+199
-0
lines changed

9 files changed

+199
-0
lines changed

spring-boot-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<module>spring-boot-brave</module>
134134
<module>spring-boot-simple</module>
135135
<module>spring-boot-http2</module>
136+
<module>spring-boot-testing-5</module>
136137
</modules>
137138

138139
<build>
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.mockitospytest;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
8+
9+
import static org.mockito.ArgumentMatchers.any;
10+
import static org.mockito.Mockito.doReturn;
11+
import static org.mockito.Mockito.verify;
12+
13+
@SpringBootTest
14+
class OrderServiceIntegrationTest {
15+
16+
@Autowired
17+
OrderRepository orderRepository;
18+
@MockitoSpyBean
19+
NotificationService notificationService;
20+
@MockitoSpyBean
21+
OrderService orderService;
22+
23+
@Test
24+
void givenNotificationServiceIsUsingSpyBean_whenOrderServiceIsCalled_thenNotificationServiceSpyBeanShouldBeInvoked() {
25+
26+
Order orderInput = new Order(null, "Test", 1.0, "17 St Andrews Croft, Leeds ,LS17 7TP");
27+
doReturn(true).when(notificationService)
28+
.raiseAlert(any(Order.class));
29+
Order order = orderService.save(orderInput);
30+
Assertions.assertNotNull(order);
31+
Assertions.assertNotNull(order.getId());
32+
verify(notificationService).notify(any(Order.class));
33+
}
34+
35+
}

0 commit comments

Comments
 (0)