Make mockito less strict with object comparison

Viewed 2143

I have the following code:

public JSONObject addProductToOrder(int orderId, int productId, String name, double price) {
    JSONObject json = new JSONObject();
    try {
        json.put("orderId", orderId);
        json.put("productId", productId);
        json.put("name", name);
        json.put("price", price);
    } catch (JSONException e) {
        Debugger.out(e);
    }
    return this.dataSource.write("/orderItems/addproductToOrder", json);
}

I want to test so using Mockito and I have done the following:

public void getOrder() throws JSONException {
    JSONObject json = new JSONObject("{result: 'OK', error: 0}");
    doReturn(json)
            .when(this.clientSpy) // this.clientSpy = Mockito.spy(client);
            .post("/orderItems/addProductToOrder", new JSONObject("{productId: 1, orderId: 1, price: 15.99, name: 'Product 1'}"));

    OrderItem orderItem = new OrderItem(this.api);
    assertEquals("OK", orderItem.addProductToOrder(1, 1, "Product 1", 15.99).get("result"));
}

The way I understand things, my doReturn() is not triggered because new JSONObject() !== new JSONObject().

Is there a way to make it not compare the objects, but instead just the contents?

1 Answers
Related