{"id":4703,"date":"2022-11-20T11:02:51","date_gmt":"2022-11-20T11:02:51","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4703"},"modified":"2022-11-20T11:05:34","modified_gmt":"2022-11-20T11:05:34","slug":"mock-mvc-testing","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/mock-mvc-testing\/","title":{"rendered":"Mock MVC Testing"},"content":{"rendered":"<p><strong class=\"ctaHeader3\">POST<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n @Test\r\n    void addEmployeeDetails() throws Exception {\r\n        Employee objEmployee = (Employee) TestTraits.getObjfromJson(&quot;json-data\/EmployeeObject.json&quot;);\r\n\r\n        String jsonRequestString = mapper.writeValueAsString(objEmployee);\r\n\r\n        when(employeeServiceImpl.saveEmployee(any())).thenReturn(&quot;101&quot;);\r\n\r\n        ResultActions objResultActions = this.mockMvc.perform(post(&quot;\/empmgmt\/employees&quot;)\r\n                .contentType(MediaType.APPLICATION_JSON)\r\n                .content(jsonRequestString))\r\n                .andDo(print())\r\n                .andExpect(jsonPath(&quot;$.empID&quot;, is(Matchers.notNullValue())))\r\n                .andExpect(jsonPath(&quot;$.empID&quot;, is(101)))\r\n                .andExpect(jsonPath(&quot;$.empName&quot;, is(&quot;Mani&quot;)))\r\n                .andExpect(status().isCreated());\r\n    }\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">GET<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n @Test\r\n    void getEmployeeDetails() throws Exception {\r\n        ClassLoader loader = Test.class.getClassLoader();\r\n        Employee objEmployee = (Employee) TestTraits.getObjfromJson(&quot;json-data\/EmployeeObject.json&quot;);\r\n\r\n        Optional&lt;Employee&gt; objEmp = Optional.ofNullable(objEmployee);\r\n        when(employeeServiceImpl.findEmployee(EMP_ID)).thenReturn(objEmp);\r\n\r\n        ResultActions objResultActions = this.mockMvc.perform(get(&quot;\/empmgmt\/employees\/{empID}&quot;,EMP_ID))\r\n                                                    .andDo(print())\r\n                                                    .andExpect(jsonPath(&quot;$.empID&quot;, is(101)))\r\n                                                    .andExpect(jsonPath(&quot;$.empName&quot;, is(&quot;Mani&quot;)))\r\n                                                    .andExpect(jsonPath(&quot;$._links.all-employees.href&quot;, is(&quot;http:\/\/localhost\/empmgmt\/employees&quot;)))\r\n                                                    .andExpect(status().isOk());\r\n    }\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">DELETE<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n @Test\r\n    void deleteEmployeeDetails() throws Exception {\r\n        when(employeeServiceImpl.deleteEmployee(Long.valueOf(EMP_ID))).thenReturn(true);\r\n\r\n        this.mockMvc.perform(delete(&quot;\/empmgmt\/employees\/{empID}&quot;,EMP_ID))\r\n                .andDo(print())\r\n                .andExpect(content().string(containsString(&quot;Employee Deleted Successfully&quot;)))\r\n                .andExpect(status().isOk());\r\n    }\r\n<\/pre>\n<p><strong>EmployeeControllerTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.org.controllers;\r\n\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\nimport com.mugil.org.TestTraits;\r\nimport com.mugil.org.models.Employee;\r\nimport com.mugil.org.services.EmployeeServiceImpl;\r\nimport org.hamcrest.Matchers;\r\nimport org.junit.jupiter.api.Test;\r\nimport org.junit.jupiter.api.extension.ExtendWith;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;\r\nimport org.springframework.boot.test.mock.mockito.MockBean;\r\nimport org.springframework.http.MediaType;\r\nimport org.springframework.test.context.junit.jupiter.SpringExtension;\r\nimport org.springframework.test.web.servlet.MockMvc;\r\nimport org.springframework.test.web.servlet.ResultActions;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\nimport java.util.Optional;\r\n\r\nimport static org.hamcrest.Matchers.containsString;\r\nimport static org.hamcrest.Matchers.is;\r\nimport static org.mockito.ArgumentMatchers.any;\r\nimport static org.mockito.Mockito.when;\r\nimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;\r\nimport static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;\r\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;\r\n\r\n@ExtendWith(SpringExtension.class)\r\n@WebMvcTest(EmployeeController.class)\r\nclass EmployeeControllerTest {\r\n\r\n    @MockBean\r\n    EmployeeServiceImpl employeeServiceImpl;\r\n\r\n    ObjectMapper mapper = new ObjectMapper();\r\n\r\n    @Autowired\r\n    private MockMvc mockMvc;\r\n\r\n    private static final String EMP_ID = &quot;101&quot;;\r\n\r\n    @Test\r\n    void getEmployeeDetails() throws Exception {\r\n        ClassLoader loader = Test.class.getClassLoader();\r\n        Employee objEmployee = (Employee) TestTraits.getObjfromJson(&quot;json-data\/EmployeeObject.json&quot;);\r\n\r\n        Optional&lt;Employee&gt; objEmp = Optional.ofNullable(objEmployee);\r\n        when(employeeServiceImpl.findEmployee(EMP_ID)).thenReturn(objEmp);\r\n\r\n        ResultActions objResultActions = this.mockMvc.perform(get(&quot;\/empmgmt\/employees\/{empID}&quot;,EMP_ID))\r\n                                                    .andDo(print())\r\n                                                    .andExpect(jsonPath(&quot;$.empID&quot;, is(101)))\r\n                                                    .andExpect(jsonPath(&quot;$.empName&quot;, is(&quot;Mani&quot;)))\r\n                                                    .andExpect(jsonPath(&quot;$._links.all-employees.href&quot;, is(&quot;http:\/\/localhost\/empmgmt\/employees&quot;)))\r\n                                                    .andExpect(status().isOk());\r\n    }\r\n\r\n    @Test\r\n    void deleteEmployeeDetails() throws Exception {\r\n        when(employeeServiceImpl.deleteEmployee(Long.valueOf(EMP_ID))).thenReturn(true);\r\n\r\n        this.mockMvc.perform(delete(&quot;\/empmgmt\/employees\/{empID}&quot;,EMP_ID))\r\n                .andDo(print())\r\n                .andExpect(content().string(containsString(&quot;Employee Deleted Successfully&quot;)))\r\n                .andExpect(status().isOk());\r\n    }\r\n\r\n    @Test\r\n    void getAllEmployeeDetails() throws Exception {\r\n        List&lt;Employee&gt; arrEmployees = new ArrayList&lt;&gt;();\r\n        arrEmployees.add(getEmployee());\r\n\r\n        when(employeeServiceImpl.findAllEmployee()).thenReturn(arrEmployees);\r\n\r\n        ResultActions objResultActions = this.mockMvc.perform(get(&quot;\/empmgmt\/employees&quot;)\r\n                .contentType(MediaType.APPLICATION_JSON))\r\n                .andDo(print())\r\n                .andExpect(jsonPath(&quot;$&#x5B;0].empID&quot;, is(Matchers.notNullValue())))\r\n                .andExpect(jsonPath(&quot;$&#x5B;0].empID&quot;, is(101)))\r\n                .andExpect(jsonPath(&quot;$&#x5B;0].empName&quot;, is(&quot;Shivaji&quot;)))\r\n                .andExpect(status().is2xxSuccessful());\r\n    }\r\n\r\n    @Test\r\n    void addEmployeeDetails() throws Exception {\r\n        Employee objEmployee = (Employee) TestTraits.getObjfromJson(&quot;json-data\/EmployeeObject.json&quot;);\r\n\r\n        String jsonRequestString = mapper.writeValueAsString(objEmployee);\r\n\r\n        when(employeeServiceImpl.saveEmployee(any())).thenReturn(&quot;101&quot;);\r\n\r\n        ResultActions objResultActions = this.mockMvc.perform(post(&quot;\/empmgmt\/employees&quot;)\r\n                .contentType(MediaType.APPLICATION_JSON)\r\n                .content(jsonRequestString))\r\n                .andDo(print())\r\n                .andExpect(jsonPath(&quot;$.empID&quot;, is(Matchers.notNullValue())))\r\n                .andExpect(jsonPath(&quot;$.empID&quot;, is(101)))\r\n                .andExpect(jsonPath(&quot;$.empName&quot;, is(&quot;Mani&quot;)))\r\n                .andExpect(status().isCreated());\r\n    }\r\n\r\n    private Employee getEmployee(){\r\n        Employee objEmployee = new Employee();\r\n\r\n        objEmployee.setEmpID(Long.valueOf(101));\r\n        objEmployee.setEmpAge(&quot;35&quot;);\r\n        objEmployee.setEmpName(&quot;Shivaji&quot;);\r\n\r\n        return objEmployee;\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>POST @Test void addEmployeeDetails() throws Exception { Employee objEmployee = (Employee) TestTraits.getObjfromJson(&quot;json-data\/EmployeeObject.json&quot;); String jsonRequestString = mapper.writeValueAsString(objEmployee); when(employeeServiceImpl.saveEmployee(any())).thenReturn(&quot;101&quot;); ResultActions objResultActions = this.mockMvc.perform(post(&quot;\/empmgmt\/employees&quot;) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequestString)) .andDo(print()) .andExpect(jsonPath(&quot;$.empID&quot;, is(Matchers.notNullValue()))) .andExpect(jsonPath(&quot;$.empID&quot;, is(101))) .andExpect(jsonPath(&quot;$.empName&quot;, is(&quot;Mani&quot;))) .andExpect(status().isCreated()); } GET @Test void getEmployeeDetails() throws Exception { ClassLoader loader = Test.class.getClassLoader(); Employee objEmployee = (Employee) TestTraits.getObjfromJson(&quot;json-data\/EmployeeObject.json&quot;); Optional&lt;Employee&gt; objEmp = Optional.ofNullable(objEmployee); when(employeeServiceImpl.findEmployee(EMP_ID)).thenReturn(objEmp); ResultActions objResultActions&hellip; <a href=\"https:\/\/codethataint.com\/blog\/mock-mvc-testing\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[341],"tags":[],"class_list":["post-4703","post","type-post","status-publish","format-standard","hentry","category-mockmvc"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4703"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4703\/revisions"}],"predecessor-version":[{"id":4705,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4703\/revisions\/4705"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}