mardi 26 mars 2019

Spring Boot testing coverage - cover return Gson statement

I have a Spring Boot project that I'm testing and I have this get method in the controller:

 @GetMapping("/updatecrime/{id}")
    public String updateCrime(@PathVariable Long id) {
        Crime oldCrime = new Crime(id);
        this.service.addCrime(id, oldCrime);
        Crime newCrime = new Crime(oldCrime.getId(), oldCrime.getZipCode(), oldCrime.getTotPopulation(),
                oldCrime.getMedianAge(), oldCrime.getTotMales(), 10, oldCrime.getTotHouseholds(),
                oldCrime.getAvgHouseholdSize());
        return new Gson().toJson(this.service.updateCrime(id, oldCrime, newCrime));
    }

I checked the coverage of my tests and all this method is covered expected for this last line:

return new Gson().toJson(this.service.updateCrime(id, oldCrime, newCrime))

What kind of assert do I need to cover this? This is my test for the method:

this.objectMapper = new ObjectMapper();
        try {
            ResultActions resultActions = this.mvc
                    .perform(MockMvcRequestBuilders.get("/updatecrime/5"));
            MvcResult result = resultActions.andReturn();

            String contentString = result.getResponse().getContentAsString();
            Crime crime = objectMapper.readValue(contentString, Crime.class);
            assertTrue(crime.getTotFemales() == 10);
        } catch (Exception e) {
            e.printStackTrace();
        }

Aucun commentaire:

Enregistrer un commentaire