Categories
Mastering Development

AssertionError: No value at JSON path, but value exists in valid JSON response

I saw many of solutions provided here, on the forum, and also a guide for the problem (provided here), but none of them helped me…

I tried to keep my code cleare as much as I can, so I decided to create many of test classes including test items, for example:

@Getter
@Setter
public class SalesTaskTestItem {

    public static SalesTask buildTestItem(){

        List<ContactPerson> contactPersonTestItemList = new ArrayList<>();
        contactPersonTestItemList.add(ContactPersonTestItem.buildTestItem());

        List<SalesMan> supportingSalesTeamList = new ArrayList<>();

        List<Product> discussedProductsTestList = new ArrayList<>();
        discussedProductsTestList.add(ProductTestItem.buildTestItem());

        List<SalesTaskProgress> progressTestList = new ArrayList<>();
        progressTestList.add(SalesTaskProgressTestItem.buildTestItemNo1());
        progressTestList.add(SalesTaskProgressTestItem.buildTestItemNo2());
        progressTestList.add(SalesTaskProgressTestItem.buildTestItemNo3());

        List<Offer> alreadySentOffersTestList = new ArrayList<>();
        alreadySentOffersTestList.add(OfferTestItem.buildTestItem());

        List<AssignedTaskDocument> assignedTaskDocumentsTestList = new ArrayList<>();
        assignedTaskDocumentsTestList.add(AssignedTaskDocumentTestItem.buildTestItem());

        List<InternalProcedureDocument> internalProceduresDocumentsTestList = new ArrayList<>();
        internalProceduresDocumentsTestList.add(InternalProcedureDocumentTestItem.buildTestItem());


        SalesTask testItem = new SalesTask();
        testItem.setId(1L);
        testItem.setVersion(1);
        testItem.setTaskEstablishedDate(DateFormatter.fromStringToDate("10-12-2020T09:12:45"));
        testItem.setLastProgressDate(DateFormatter.fromStringToDate("10-12-2020T09:30:56"));
        testItem.setCompany(CompanyTestItem.buildTestItem());
        testItem.setContactPersonsList(contactPersonTestItemList);
        testItem.setMainSalesMan(SalesManTestItem.buildTestItemNo1());
        testItem.setSupportingSalesTeam(supportingSalesTeamList);
        testItem.setDiscussedProducts(discussedProductsTestList);
        testItem.setProgressList(progressTestList);
        testItem.setCurrentTaskValue(BigDecimal.valueOf(250000));
        testItem.setChanceOfPositiveFinishingTask(0.45);
        testItem.setEstimatedDateOfFinishingTask(DateFormatter.fromStringToDate("30-12-2020T13:00:00"));
        testItem.setAlreadySentOffersList(alreadySentOffersTestList);
        testItem.setAssignedTaskDocumentsList(assignedTaskDocumentsTestList);
        testItem.setInternalProceduresDocumentsList(internalProceduresDocumentsTestList);
        return testItem;
    }

}

My test case is:

@Test
public void createNewSalesTask(){

    SalesTask newSalesTask = new SalesTask();
    newSalesTask = SalesTaskTestItem.buildTestItem();

    when(salesTaskService.save(any(SalesTask.class))).thenReturn(newSalesTask);

    ObjectMapper objectMapper = new ObjectMapper();

    try {
        String newSalesTaskJSON = objectMapper.writeValueAsString(newSalesTask);

        ResultActions resultActions = mockMvc.perform(
                post("/salesTask")
                .contentType(MediaType.APPLICATION_JSON)
                .content(newSalesTaskJSON)
        );

        resultActions.andExpect(status().isCreated())
                .andDo(print())
                .andExpect(content().contentType("application/json"))
                .andExpect(jsonPath("$.id").value(1L))
                .andExpect(jsonPath("$.version").value(1))
                .andExpect(jsonPath("$.taskEstablishedDate").value(SalesTaskTestItem.buildTestItem().getTaskEstablishedDate()))
                .andExpect(jsonPath("$.lastProgressDate").value(SalesTaskTestItem.buildTestItem().getLastProgressDate()))
                .andExpect(jsonPath("$.company").value(SalesTaskTestItem.buildTestItem().getCompany()))
                .andExpect(jsonPath("$.contactPersonsList").value(SalesTaskTestItem.buildTestItem().getContactPersonsList()))
                .andExpect(jsonPath("$.mainSalesMan").value(SalesTaskTestItem.buildTestItem().getMainSalesMan()))
                .andExpect(jsonPath("$.supportingSalesTeam").value(SalesTaskTestItem.buildTestItem().getSupportingSalesTeam()))
                .andExpect(jsonPath("$.discussedProducts").value(SalesTaskTestItem.buildTestItem().getDiscussedProducts()))
                .andExpect(jsonPath("$.progressList").value(SalesTaskTestItem.buildTestItem().getProgressList()))
                .andExpect(jsonPath("$.currentTaskValue").value(SalesTaskTestItem.buildTestItem().getCurrentTaskValue()))
                .andExpect(jsonPath("$.chanceOfPositiveFinishingTask").value(SalesTaskTestItem.buildTestItem().getChanceOfPositiveFinishingTask()))
                .andExpect(jsonPath("$.estimatedDateOfFinishingTask").value(SalesTaskTestItem.buildTestItem().getEstimatedDateOfFinishingTask()))
                .andExpect(jsonPath("$.alreadySentOffersList").value(SalesTaskTestItem.buildTestItem().getAlreadySentOffersList()))
                .andExpect(jsonPath("$.assignedTaskDocumentsList").value(SalesTaskTestItem.buildTestItem().getAssignedTaskDocumentsList()))
                .andExpect(jsonPath("$.internalProceduresDocumentsList").value(SalesTaskTestItem.buildTestItem().getInternalProceduresDocumentsList()));

    } catch (Exception e) {
        e.printStackTrace();
    }

}

I see in stack trace my MockHttpServletResponse, where I can find "taskEstablishedDate":"2020-12-10T09:12:45.000+00:00". Also, If I copy whole body of this response and past it into https://jsonformatter.curiousconcept.com/, validation is passed with no problems.

MockHttpServletResponse:
           Status = 201
    Error message = null
          Headers = [Content-Type:"application/json"]
     Content type = application/json
             Body = {"id":1,"version":1,"taskEstablishedDate":"2020-12-10T09:12:45.000+00:00","lastProgressDate":"2020-12-10T09:30:56.000+00:00", (...) }
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

But I get java.lang.AssertionError: No value at JSON path "$.taskEstablishedDate".

I really don’t know what is wrong. Could anybody help?

Leave a Reply

Your email address will not be published. Required fields are marked *