0

how to write test case for if else coditions.I am not getting coverage in the else block when giving the object as null. Also not getting coverage in the catch block.

if (Object!= null) {
            if (Object.getId() != null) {
                responseModel = registrationService.updateVendor(Object);
            } else {
                responseModel = registrationService.save(Object);
            }
        } else {
            responseModel = new ResponseModel();
            responseModel.setCode(ERROR_1002);
            responseModel.setMessage(ERROR_MSG1002);
            logger.info("Vendor details not found");
        }

    } catch (NullPointerException  e) {
        //e.printStackTrace();

        responseModel = new ResponseModel();
        responseModel.setCode(ERROR_1000);
        responseModel.setMessage(ERROR_MSG1000);
        logger.error("Exception inside RegistrationController.Registration()" + e);

    }
Ayisha mehrin
  • 45
  • 1
  • 6
  • could you share your test case code – Narendra Pandey Jan 29 '19 at 06:44
  • @Test public void testCreateOrderNotNull() throws Exception { String json="{ \r\n" + " \"Id\": \"1\",\r\n" + " \"swiftCode\":\"asdsdE988989\"\r\n" + " }\r\n" + " ]\r\n" + "}\r\n" + MockMvc.perform(MockMvcRequestBuilders.post("registration").contentType(MediaType.APPLICATION_JSON) .content(json)).andExpect(MockMvcResultMatchers.status().isOk()); } – Ayisha mehrin Jan 29 '19 at 07:10
  • I am assuming your string `json` will get converted to some DTO object at server end. You should try to send a null represent of object. – Narendra Pandey Jan 29 '19 at 07:32
  • @Test(expected=NullPointerException.class) public void testCreateOrderVendorDetailsNull() throws Exception { String json=null; mockMvc.perform(MockMvcRequestBuilders.post("/registration").contentType(MediaType.APPLICATION_JSON) .content(json)).andExpect(MockMvcResultMatchers.jsonPath("$.message", Matchers.is("r Details not found"))); } this test case fails – Ayisha mehrin Jan 29 '19 at 07:36
  • follow this https://stackoverflow.com/questions/21120999/representing-null-in-json – Narendra Pandey Jan 29 '19 at 07:38
  • did it worked?? – Narendra Pandey Jan 30 '19 at 09:14
  • no.It didnt worked. – Ayisha mehrin Jan 31 '19 at 05:11
  • see in your how `Object` is getting populated. Is it getting resolved from the JSON string we are passing ? – Narendra Pandey Jan 31 '19 at 07:06

0 Answers0