3

I am new to REST Assured and my objective is to create a custom Hamcrest Matcher which accepts java object(Item; as referred below) created by me.

This is the class that extends the TypeSafeMatcher abstract class.

import net.example.Item;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class ItemValidate extends TypeSafeMatcher<Item> {

    @Override
    protected boolean matchesSafely(Item item) {
        // code that verify the Item object
        return true;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(" is apple");
    }

    public static Matcher<Item> itemValidate() {
        return new ItemValidate();
    }
}

This is the Item class implementation

public class Item {

    String key_1;
    String key_2;

    public Item(String key_1, String key_2) {
        this.key_1 = key_1;
        this.key_2 = key_2;
    }

    public String getKey_1() {
        return key_1;
    }

    public void setKey_1(String key_1) {
        this.key_1 = key_1;
    }

    public String getKey_2() {
        return key_2;
    }

    public void setKey_2(String key_2) {
        this.key_2 = key_2;
    }
}

And this is the test method where I use it.

        given()
            .contentType(ContentType.JSON)
            .body(payLoad)
            .pathParam("param_1", "p1")
            .pathParam("param21", "p2")
            .when()
            .post(url + "/process")
            .then()
            .body("key_0.results[0].item", itemValidate());

This is the json response body for the above API call

    {
    "key_0": {
        "results": [
            {
                "item": {
                    "key_1": "red",
                    "key_2": "apple"
                },
                "description" : "this is a fruit"
            }
        ]
    },
    "key_3": "value_3"
   }

But I get;

java.lang.AssertionError: 1 expectation failed.
JSON path key_0.results[0].item doesn't match.
Expected:  is apple
Actual: {key_1=red, key_2=apple}

I don't understand why the jsonpath is incorrect in this. If the object type I use for custom matcher is String(I change the jsonpath to key_0.results[0].item.key_2), it works perfectly. How can I use a java object created by me in the custom matcher?

kaweesha
  • 803
  • 6
  • 16
  • Can you also show the implementation of `Item` class and method `matchesSafely(Item item)`? – Fenio Apr 29 '20 at 03:28
  • @Fenio - In `matchesSafely(Item item)` method, I had added just a `System.out.println(item.getKey_2())` I can't proceed with verification due to this error. Even I added a verification code or not this AssertionError is occurring. And I added the Item implementation to the question. – kaweesha Apr 29 '20 at 04:32
  • So, right now you always return `true` but assertion fails anyway? – Fenio Apr 29 '20 at 04:39
  • @Fenio - Yes. I just `return true`. But assertion fails. I referred [this](https://www.baeldung.com/hamcrest-custom-matchers) article to do this. He has only passed String and Integer as object type in TypeSafeMatcher. I think we can't pass other than basic data types in Java. – kaweesha Apr 29 '20 at 04:53
  • I'm sorry but I don't know what wrong here. I can't help you. – Fenio Apr 29 '20 at 07:27
  • Hey @Kaweesha, did you find a solution to this problem? – Jalil.Jarjanazy Apr 28 '22 at 15:51
  • @Jalil.Jarjanazy no I couldn't – kaweesha May 04 '22 at 12:42

0 Answers0