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?
What happens here is that Mockito is calling equals() to compare the object that you provided to the specification to the one that is used for the actual method call.
Basically, there are three options:
any()
or isNull()
. Please note: when using such matchers, all arguments must be matched, you can't do: foo(any(), 5)
for example.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With