Spring-Boot-Unit-Test-Controller-Layer and Integration Testing

Spring-Boot-Unit-Test-Controller-Layer and Integration Testing

@SpringBootTest

The @SpringBootTest annotation is used in Spring Boot applications for integration testing. It allows you to load and configure the complete application context, simulating a running application, and enabling you to test multiple layers of the application together.

@WebMvcTest

The @WebMvcTest is a Spring Boot test annotation used to test the web layer of an application. It focuses on testing the controllers by simulating HTTP requests and verifying the responses.

@MockBean

The @MockBean annotation in Spring Boot is used to create a mock implementation of a bean during testing. It is typically used in conjunction with @SpringBootTest or @WebMvcTest to mock dependencies and isolate the unit under test.

Example :

@WebMvcTest

@WebMvcTest(EmployeeController.class)
public class EmployeeControllerTest {

    @Mock
    private EmployeeService EmployeeService;

    @InjectMocks
    private EmployeeController EmployeeController;

    private MockMvc mockMvc;

    @BeforeEach
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(EmployeeController).build();
    }

    @Test
    public void testGetAllEmployees() throws Exception {
        Employee Employee1 = new Employee("1", "Employee 1", "Author 1");
        Employee Employee2 = new Employee("2", "Employee 2", "Author 2");
        List<Employee> Employees = Arrays.asList(Employee1, Employee2);

        when(EmployeeService.getAllEmployees()).thenReturn(Employees);

        mockMvc.perform(MockMvcRequestBuilders.get("/Employees")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$[0].id").value("1"))
                .andExpect(MockMvcResultMatchers.jsonPath("$[0].title").value("Employee 1"))
                .andExpect(MockMvcResultMatchers.jsonPath("$[0].author").value("Author 1"))
                .andExpect(MockMvcResultMatchers.jsonPath("$[1].id").value("2"))
                .andExpect(MockMvcResultMatchers.jsonPath("$[1].title").value("Employee 2"))
                .andExpect(MockMvcResultMatchers.jsonPath("$[1].author").value("Author 2"));
    }
}

@SpringBootTest


@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EmployeeIntegrationTest {

    @LocalServerPort
    private int port;

    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private EmployeeRepository EmployeeRepository;

    @Autowired
    private EmployeeService EmployeeService;

    @Test
    public void testCreateEmployee() {
        Employee Employee = new Employee("1", "Employee 1", "Author 1");
        HttpEntity<Employee> request = new HttpEntity<>(Employee);

        ResponseEntity<Employee> response = restTemplate.exchange(
                "http://localhost:" + port + "/Employees",
                HttpMethod.POST,
                request,
                Employee.class
        );

        Assertions.assertEquals(HttpStatus.CREATED, response.getStatusCode());
        Assertions.assertNotNull(response.getBody());

        Employee savedEmployee = EmployeeRepository.findById(response.getBody().getId()).orElse(null);
        Assertions.assertNotNull(savedEmployee);
        Assertions.assertEquals(Employee.getTitle(), savedEmployee.getTitle());
        Assertions.assertEquals(Employee.getAuthor(), savedEmployee.getAuthor());
    }

    @Test
    public void testGetAllEmployees() {
        EmployeeRepository.save(new Employee("1", "Employee 1", "Author 1"));
        EmployeeRepository.save(new Employee("2", "Employee 2", "Author 2"));

        ResponseEntity<Employee[]> response = restTemplate.getForEntity(
                "http://localhost:" + port + "/Employees",
                Employee[].class
        );

        Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
        Assertions.assertNotNull(response.getBody());
        Assertions.assertEquals(2, response.getBody().length);
    }


}