java - Make mockito less strict with object comparison -
i have 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 test using mockito , have done 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 understand things, doreturn()
not triggered because new jsonobject() !== new jsonobject()
.
is there way make not compare objects, instead contents?
what happens here mockito calling equals() compare object provided specification 1 used actual method call.
basically, there 3 options:
- if provide object, equals() used compare
- then have variety of other argumentmatchers, such
any()
orisnull()
. please note: when using such matchers, all arguments must matched, can't do:foo(any(), 5)
example. - if doesn't do: can use argumentcaptor. meaning: instead of having mockito compare objects, record object passed mock. , add step in test case check captured argument required.
Comments
Post a Comment