Spring Boot & H2 Tutorial
Selected Reading
- Spring Boot & H2 - Discussion
- Spring Boot & H2 - Useful Resources
- Spring Boot & H2 - Quick Guide
- Spring Boot & H2 - Unit Test Repository
- Spring Boot & H2 - Unit Test Service
- Spring Boot & H2 - Unit Test Controller
- Spring Boot & H2 - Delete Record
- Spring Boot & H2 - Update Record
- Spring Boot & H2 - Get All Records
- Spring Boot & H2 - Get Record
- Spring Boot & H2 - Add Record
- Spring Boot & H2 - H2 Console
- Spring Boot & H2 - REST APIs
- Spring Boot & H2 - Project Setup
- Spring Boot & H2 - Environment Setup
- Spring Boot & H2 - Overview
- Spring Boot & H2 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring Boot & H2 - Unit Test Service
Spring Boot & H2 - Unit Test Service
To test a Service, we need the following annotation and classes −
@ExtendWith(SpringExtension.class) − Mark the class to run as test case using SpringExtension class.
@SpringBootTest(classes = SprintBootH2Apppcation.class) − Configure the Spring Boot apppcation.
@MockBean private EmployeeService employeeService − EmployeeService mock object to be tested.
Following is the complete code of EmployeeServiceTest.
package com.tutorialspoint.service; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.doNothing; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.junit.jupiter.SpringExtension; import com.tutorialspoint.entity.Employee; import com.tutorialspoint.sprintbooth2.SprintBootH2Apppcation; @ExtendWith(SpringExtension.class) @SpringBootTest(classes = SprintBootH2Apppcation.class) pubpc class EmployeeServiceTest { @MockBean private EmployeeService employeeService; @Test pubpc void testGetAllEmployees() throws Exception { Employee employee = getEmployee(); List<Employee> employees = new ArrayList<>(); employees.add(employee); given(employeeService.getAllEmployees()).willReturn(employees); List<Employee> result = employeeService.getAllEmployees(); assertEquals(result.size(), 1); } @Test pubpc void testGetEmployee() throws Exception { Employee employee = getEmployee(); given(employeeService.getEmployeeById(1)).willReturn(employee); Employee result = employeeService.getEmployeeById(1); assertEquals(result.getId(), 1); } @Test pubpc void testDeleteEmployee() throws Exception { doNothing().when(employeeService).deleteEmployeeById(1); employeeService.deleteEmployeeById(1); assertTrue(true); } @Test pubpc void testSaveOrUpdateEmployee() throws Exception { Employee employee = getEmployee(); doNothing().when(employeeService).saveOrUpdate(employee); employeeService.saveOrUpdate(employee); assertTrue(true); } private Employee getEmployee() { Employee employee = new Employee(); employee.setId(1); employee.setName("Mahesh"); employee.setAge(30); employee.setEmail("mahesh@test.com"); return employee; } }
Run the test cases.
Right Cpck on the file in ecppse and select Run a JUnit Test and verify the result.
Advertisements