{"id":4693,"date":"2022-11-20T10:43:07","date_gmt":"2022-11-20T10:43:07","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4693"},"modified":"2022-11-20T11:00:34","modified_gmt":"2022-11-20T11:00:34","slug":"reading-values-from-json-file","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/reading-values-from-json-file\/","title":{"rendered":"Reading values from JSON File"},"content":{"rendered":"<p><strong>TestTraits.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.org;\r\n\r\nimport com.google.gson.Gson;\r\nimport com.mugil.org.models.Employee;\r\nimport org.springframework.core.io.ClassPathResource;\r\n\r\nimport java.io.FileReader;\r\nimport java.io.IOException;\r\nimport java.io.Reader;\r\n\r\npublic interface TestTraits {\r\n    public static Object getObjfromJson(String fileLoctaion) throws IOException {\r\n        Gson gson = new Gson();\r\n        Employee objEmployee2 = gson.fromJson(getReader(fileLoctaion), Employee.class);\r\n        return objEmployee2;\r\n    }\r\n\r\n    public static Reader getReader(String fileLoctaion) throws IOException {\r\n        return new FileReader(new ClassPathResource(fileLoctaion).getFile());\r\n    }\r\n}\r\n<\/pre>\n<p><strong>EmployeeObject.json<\/strong><\/p>\n<pre>\r\n{\r\n  \"empID\": 101,\r\n  \"empName\": \"Mani\",\r\n  \"empAge\": \"35\"\r\n}\r\n<\/pre>\n<p><strong>EmployeeControllerTest.java<\/strong><br \/>\nRead JSON Value as from File and Convert to Employee Object and to JSON String<\/p>\n<p><strong class=\"ctaHeader3\">JSON Value from File -> Employee Object -> JSON String from Object<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  @Test\r\n    void addEmployeeDetails() throws Exception {\r\n        \r\n        \/\/Read values from JSON  \r\n        Employee objEmployee = (Employee) TestTraits.getObjfromJson(&quot;json-data\/EmployeeObject.json&quot;);\r\n\r\n        \/\/Convert Object to JSON String\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>EmployeeControllerTest.java<\/strong><br \/>\nRead JSON Value as from File and Convert to Employee Object and to JSON String<\/p>\n<p><strong class=\"ctaHeader3\">JSON Value from File -> Employee Object for Mocking Service Call<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\n@Test\r\n    void getEmployeeDetails() throws Exception {\r\n          \r\n        \/\/Object Construction by reading file\r\n        Employee objEmployee = (Employee) TestTraits.getObjfromJson(&quot;json-data\/EmployeeObject.json&quot;);\r\n\r\n        \/\/Making object as Optional\r\n        Optional&lt;Employee&gt; objEmp = Optional.ofNullable(objEmployee);\r\n\r\n        \/\/Mocked service response from object created\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><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2022\/11\/FileStructurefortesting.png\" alt=\"\" \/><\/p>\n<p><strong>Repo Link<\/strong><br \/>\n<a href=\"https:\/\/bitbucket.org\/Mugil\/restwithspringboot\/branch\/feature\/7MockMVCTesting_3\">https:\/\/bitbucket.org\/Mugil\/restwithspringboot\/branch\/feature\/7MockMVCTesting_3<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>TestTraits.java package com.mugil.org; import com.google.gson.Gson; import com.mugil.org.models.Employee; import org.springframework.core.io.ClassPathResource; import java.io.FileReader; import java.io.IOException; import java.io.Reader; public interface TestTraits { public static Object getObjfromJson(String fileLoctaion) throws IOException { Gson gson = new Gson(); Employee objEmployee2 = gson.fromJson(getReader(fileLoctaion), Employee.class); return objEmployee2; } public static Reader getReader(String fileLoctaion) throws IOException { return new FileReader(new ClassPathResource(fileLoctaion).getFile()); } } EmployeeObject.json&hellip; <a href=\"https:\/\/codethataint.com\/blog\/reading-values-from-json-file\/\">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":[47],"tags":[],"class_list":["post-4693","post","type-post","status-publish","format-standard","hentry","category-testing"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4693","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=4693"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4693\/revisions"}],"predecessor-version":[{"id":4702,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4693\/revisions\/4702"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}