Professional Documents
Culture Documents
Mockbean 1
Mockbean 1
ConcretePage.com
On this page we will learn using @MockBean annotation in Spring Boot unit test cases. Let us understand @MockBean
point-by-point.
1. The @MockBean is a Spring Boot test annotation that is used to add mocks to ApplicationContext.
2. A mock will replace existing bean of the same type defined in the context and if no existing bean then new one will
be added to context.
3. The @MockBean can be used at field level and class level in unit test classes. The @MockBean can also be used in
@Configuration classes.
4. Mocks can be registered by type or bean name.
5. If a registered bean in application context is mocked then injection of this bean on field is also mocked.
6. The test class using @MockBean, is annotated with
@RunWith(SpringRunner.class)
Or
@ExtendWith(SpringExtension.class)
Contents
Technologies Used
Maven Dependencies
References
Technologies Used
Find the technologies being used in our example.
1. Java 14
2. Spring 5.3.6
3. Spring Boot 2.4.5
4. JUnit 5.7.1
@MockBean Example in Spring Test https://www.concretepage.com/spring-5/mockbean-example-spring-test
5. Mockito 3.6.28
6. Maven 3.8.1
Maven Dependencies
Find the Maven dependencies.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
</parent>
<properties>
<context.path>spring-app</context.path>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
package com.concretepage;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.concretepage.config.AppConfig;
import com.concretepage.service.MyService1;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = AppConfig.class)
public class MyAppTest1 {
@MockBean
private MyService1 myService;
@Test
public void testApp1() {
Mockito.when(myService.getMessage()).thenReturn("Welcome");
2 of 7 4/2/2022, 11:40 PM
@MockBean Example in Spring Test https://www.concretepage.com/spring-5/mockbean-example-spring-test
We can see that getMessage() method of MyService1 class has been mocked.
If a service class is using a bean which has been mocked, then that service will receive mocked bean in our test class.
MyAppTest1.java
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = AppConfig.class)
public class MyAppTest1 {
@MockBean
private MyService1 myService1;
@MockBean
private MyService2 myService2;
@Autowired
private UserService userService;
@Test
public void testApp1() {
Mockito.when(myService1.getMessage()).thenReturn("Welcome");
String msg = myService1.getMessage();
assertEquals("Welcome", msg);
}
@Test
public void testApp2() {
Mockito.when(myService2.getCount()).thenReturn(100);
int count = userService.getUserCount();
assertEquals(100, count);
}
}
In the second test method, the getCount() method of MyService2 has been mocked to return 100. The getCount() is
being called by getUserCount() of UserService. In the test result, we can see that getUserCount() will call mocked
getCount() method.
Find the classes used in our unit test class.
MyService1.java
@Service
public class MyService1 {
public String getMessage() {
return "Hello World!";
}
}
MyService2.java
@Service
public class MyService2 {
public int getCount() {
return 50;
}
}
UserService.java
@Service
public class UserService {
@Autowired
MyService2 myService;
3 of 7 4/2/2022, 11:40 PM
@MockBean Example in Spring Test https://www.concretepage.com/spring-5/mockbean-example-spring-test
@ExtendWith(SpringExtension.class)
@MockBean({MyService1.class, MyService2.class})
public class MyAppTest2 {
------
}
@ExtendWith(SpringExtension.class)
@MockBean(MyService1.class)
@MockBean(MyService2.class)
public class MyAppTest2 {
------
}
3. Using @MockBeans. The @MockBeans is the container annotation that aggregates several @MockBean annotations.
@ExtendWith(SpringExtension.class)
@MockBeans({
@MockBean(MyService1.class),
@MockBean(MyService2.class)
})
public class MyAppTest2 {
------
}
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = AppConfig.class)
@MockBeans({
@MockBean(MyService1.class),
@MockBean(MyService2.class)
})
public class MyAppTest2 {
@Autowired
private MyService1 myService1;
@Autowired
private MyService2 myService2;
@Test
public void testApp1() {
Mockito.when(myService1.getMessage()).thenReturn("Welcome");
String msg = myService1.getMessage();
assertEquals("Welcome", msg);
}
@Test
public void testApp2() {
Mockito.when(myService2.getCount()).thenReturn(100);
int count = myService2.getCount();
assertEquals(100, count);
}
}
@ExtendWith(SpringExtension.class)
4 of 7 4/2/2022, 11:40 PM
@MockBean Example in Spring Test https://www.concretepage.com/spring-5/mockbean-example-spring-test
@ContextConfiguration(classes = AppTestConfig.class)
public class MyAppTest3 {
@Autowired
private MyService1 myService1;
@Autowired
private MyService2 myService2;
@Test
public void testApp1() {
Mockito.when(myService1.getMessage()).thenReturn("Welcome");
String msg = myService1.getMessage();
assertEquals("Welcome", msg);
}
@Test
public void testApp2() {
Mockito.when(myService2.getCount()).thenReturn(100);
int count = myService2.getCount();
assertEquals(100, count);
}
}
@Configuration
class AppTestConfig {
@MockBean
private MyService1 myService1;
@MockBean
private MyService2 myService2;
}
We can see that two service classes have been mocked in configuration class. In our test class the mocked classes will
be injected using @Autowired annotation.
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = AppConfig.class)
public class MyAppTest4 {
@MockBean
@Qualifier("deer")
private Animal animal1;
@MockBean
@Qualifier("fox")
private Animal animal2;
@Test
public void testApp1() {
Mockito.when(animal1.getName()).thenReturn("xxx");
assertEquals("xxx", animal1.getName());
}
@Test
public void testApp2() {
Mockito.when(animal2.getName()).thenReturn("yyy");
assertEquals("yyy", animal2.getName());
}
}
Deer.java
@Component("deer")
public class Deer implements Animal {
5 of 7 4/2/2022, 11:40 PM
@MockBean Example in Spring Test https://www.concretepage.com/spring-5/mockbean-example-spring-test
@Override
public String getName() {
return "Deer";
}
}
Fox.java
@Component("fox")
public class Fox implements Animal {
@Override
public String getName() {
return "Fox";
}
}
References
Annotation Type MockBean
Annotation Type MockBeans
mockbean-example-spring-test.zip
POSTED BY
ARVIND RAI
Login
6 of 7 4/2/2022, 11:40 PM
@MockBean Example in Spring Test https://www.concretepage.com/spring-5/mockbean-example-spring-test
7 of 7 4/2/2022, 11:40 PM