Possible way of Asserting object in REST API’s

Asserting Actual and Expected values in an object using softAssert

    public static void assertPet(PetsRequestDTO requestDTO, PetsResponseDTO responseDTO) {
        SoftAssert softAssert = new SoftAssert();
        softAssert.assertEquals(requestDTO.getId(), responseDTO.getId());
        softAssert.assertEquals(requestDTO.getCategory(), responseDTO.getCategory());
        softAssert.assertEquals(requestDTO.getName(), responseDTO.getName());
        softAssert.assertEquals(requestDTO.getPhotoUrls(), responseDTO.getPhotoUrls());
        softAssert.assertEquals(requestDTO.getTags(), responseDTO.getTags());
        softAssert.assertEquals(requestDTO.getStatus(), responseDTO.getStatus());
        softAssert.assertAll();
    }

Assert using stream and reduce code duplicate

used gson library for deserialization.

Convert the response object to Map and do the assertion

    public static Map<Object, Object> convertDtoToMap(Object object) {
        String json = convertDtoToJson(object);
        return gson.fromJson(json, Map.class);
    }

    public static void assertCommonObject(Map<Object, Object> actual, Map<Object, Object> expected) {
        SoftAssert softAssert = new SoftAssert();
        actual.entrySet().stream().forEach(
                (e1) -> {
                    Object key = e1.getKey();
                    if (expected.containsKey(key)) {
                        softAssert.assertEquals(actual.get(key), expected.get(key), "Actual:" + actual.get(key) + " Expected:" + expected.get(key));
                    }
                }
        );
        softAssert.assertAll();
    }

Scroll to Top