Saturday, April 6, 2019

JUnit Testing REST Services and Spring MVC

1. How to write JUnit test case For Rest Web Services ?
Answer. 

package com.example.app.controller;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.example.app.model.EmployeeType;
import com.example.app.service.EmployeeTypeService;

@RunWith(SpringRunner.class)
@WebMvcTest(value = EmployeeTypeController.class, secure = false)
public class EmployeeTypeControllerTest {

 @Autowired
 private MockMvc mockMvc;

 @MockBean
 private EmployeeTypeService employeeTypeService;

 @InjectMocks
 private EmployeeTypeController employeeTypeController;

 @Test
 public void saveTest() throws Exception {

  EmployeeType employeeType = new EmployeeType(1, "FullTime");

  Mockito.when(employeeTypeService.save(Mockito.any())).thenReturn(employeeType);

  RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/employeetype/create")
    .contentType(MediaType.APPLICATION_JSON).content("{\"empTypeId\" : 0, \"empTypeName\" : \"FullTime\" }")
    .accept(MediaType.APPLICATION_JSON);

  MvcResult result = mockMvc.perform(requestBuilder).andReturn();

  System.out.println(result.getResponse());
  String expected = "{empTypeId:1,empTypeName:FullTime}";

  JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false);
  int status = result.getResponse().getStatus();
  assertEquals(200, status);
 }

 @Test
 public void findAllTest() throws Exception {

  List<EmployeeType> employeeTypes = Arrays.asList(new EmployeeType(1, "FullTime"),
    new EmployeeType(2, "PartTime"));

  Mockito.when(employeeTypeService.findAll()).thenReturn(employeeTypes);

  RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/employeetype/getEmployeeTypes")
    .accept(MediaType.APPLICATION_JSON);

  MvcResult result = mockMvc.perform(requestBuilder).andReturn();

  System.out.println(result.getResponse());
  String expected = "[{\"empTypeId\":1,\"empTypeName\":\"FullTime\"},{\"empTypeId\":2,\"empTypeName\":\"PartTime\"}]";

  JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false);
  int status = result.getResponse().getStatus();
  assertEquals(200, status);
 }

 @Test
 public void getEmployeeTypeByIdTest() throws Exception {

  EmployeeType employeeType = new EmployeeType(1, "FullTime");

  Mockito.when(employeeTypeService.findEmployeeTypeByEmpTypeId(Mockito.anyLong())).thenReturn(employeeType);

  RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/employeetype/getEmployeeType/1")
    .accept(MediaType.APPLICATION_JSON);

  MvcResult result = mockMvc.perform(requestBuilder).andReturn();

  System.out.println(result.getResponse());
  String expected = "{empTypeId:1,empTypeName:FullTime}";

  JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false);
  int status = result.getResponse().getStatus();
  assertEquals(200, status);
 }

 @Test
 public void deleteTest() throws Exception {

  RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/employeetype/delete/1")
    .accept(MediaType.APPLICATION_JSON);
  MvcResult result = mockMvc.perform(requestBuilder).andReturn();
  int status = result.getResponse().getStatus();
  assertEquals(200, status);
  String expected = "Success";
  String content = result.getResponse().getContentAsString();
  assertEquals(content, expected);
 }

}

2. How to write JUnit test case for void method ?
Answer:- 

3. How to write JUnit test case for static method ?
Answer:- 

4. How to wtite JUnit test case for singletone class ?
Answer:- 

5. How to wtite JUnit test case for static class ?
Answer:- 

6. Write Unit test case for Spring Boot REST API ?
Answer :- 

7. How to write unit test case for controller class using mockito ?
Answer:- 

8. How to write unit test case for service class using mockito ?
Answer :- 

9. How to write unit test case for DAO layer ?
Answer:- 

10. How to do junit integration test database ?
Answer:- 


Related Tutorial

No comments:

Post a Comment