Spring Boot Junit 5 Mockito
By AmarSivas | | Updated : 2022-05-19 | Viewed : 148 times

We will learn how to set up the
Table of Contents:
Set up Maven Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

Here I am going to use a previous example Spring Boot Rest Api Crud Example to add test cases using Junit and Mockito libraries.
Spring Boot Rest Controller Unit Test Example
Spring provides two annotations to write the unit test cases that we used to test the controller layer. We can use
@SpringBootTest vs @WebMvcTest
We write the unit test cases for controller. Please see the below given examples.
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringBootJunit5MockitoExampleAppTests1 {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
ProductsController productsController;
@Test
public void contextLoads() {
}
@Test
public void testWeb_WhenSuccessIsResult() throws Exception {
assertThat(testRestTemplate.getForObject("http://localhost:" + port + "/curd-app/api/testWeb", String.class)).contains("Result: Success");
}
}
We can pass the different types of parameters into @SpringBootTest. You can refer this to know more about @SpringBootTest. Here we passed the random port to webEnvironment.
We can add and configure the
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
public class SpringBootJunit5MockitoExampleAppTests2 {
@Autowired
private MockMvc mockMvc;
@Test
public void testWeb_WhenSuccessIsResult() throws Exception {
mockMvc.perform(get("/curd-app/api/testWeb")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Result: Success")));
}
}
We will look into
@ExtendWith(SpringExtension.class)
@WebMvcTest(ProductsController.class)
public class SpringBootJunit5MockitoExampleAppTests3 {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductServiceImpl productServiceImpl;
private List<Product> productList;
private Product product;
private String requestJson;
@BeforeEach
void setUp() throws JsonProcessingException {
//set up the products and product
}
@Test
public void testWeb_WhenSuccessIsResult() throws Exception {
this.mockMvc.perform(get("/curd-app/api/testWeb")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Result: Success")));
}
@Test
public void givenProducts_whenGetProducts_thenReturnJsonArray() throws Exception {
given(productServiceImpl.getAllProducts()).willReturn(new ResponseEntity(productList, HttpStatus.OK));
this.mockMvc.perform(get("/curd-app/api/products").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(containsString("productVendor2")));
}
@Test
public void testSaveProduct_WhenProduct_NotNull() throws Exception {
this.mockMvc.perform(
post("/curd-app/api/products")
.contentType(MediaType.APPLICATION_JSON).content(requestJson))
.andDo(print())
.andExpect(status().isOk());
}
}
You can find more unit test cases for different scenarios. Please observe the below above code.
Spring Boot Service Unit Test Example
We will write now the test cases for service to cover different scenarios here. Please see the below example for service layer unit tests.
@ExtendWith(MockitoExtension.class)
public class ProductServiceImplTest {
@InjectMocks
ProductServiceImpl productServiceImpl;
@Mock
ProductRepository productRepository;
List productsExpected;
Product productExpected;
@BeforeEach
public void setUp() throws IOException {
productsExpected = TestUtils.getProducts();
productExpected = (Product) productsExpected.get(0);
}
@Test
public void getAllProductsTest() throws IOException {
when(productRepository.findAll()).thenReturn(productsExpected);
ResponseEntity responseEntityExpected = productServiceImpl.getAllProducts();
Object objectReal = responseEntityExpected.getBody();
Product productReal = null;
if(objectReal instanceof List){
List productsReal = (List)objectReal;
productReal = (Product) productsReal.get(0);
}
assertNotNull(productReal);
assert(productReal.getProductVendorName().equals(productExpected.getProductVendorName()));
}
}
Spring Boot Repository Unit Test Example
We will write and test now the repository using
@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
public class ProductRepositoryTest {
@Autowired
ProductRepository productRepository;
@Test
void saveProduct_success(){
Product product = new Product();
product.setProductId(101l);
product.setProductName("testName");
product.setProductVendorName("testVendorName");
Product productResult = productRepository.save(product);
assertThat(productResult).isNotNull();
}
@Test
void saveAll_success() {
List<Product> products = Arrays.asList(
new Product(1001L,"product1", "vendor1"),
new Product(1002L,"product2", "vendor2"),
new Product(1003L,"product3", "vendor3")
);
Iterable<Product> allProducts = productRepository.saveAll(products);
assertThat(allProducts).hasSize(3);
}
@Test
public void findAll_success() {
Iterable<Product> products = productRepository.findAll();
assertThat(products).isNotEmpty();
}
@Test
public void deleteAll_success() {
productRepository.deleteAll();
assertThat(productRepository.findAll()).isEmpty();
}
}
Please find the GitHub repo for this tutorial here Spring-Boot-Junit5-Mockito-Example-App