mercredi 3 mai 2017

Unit test failing with Junit and Hamcrest - Not able to compare data with two List objects

I have the following test that is actually asserting the data within two Lists. But even though the data is same, the test does not pass. I googled and found SO links which pointed to using assertThat(actual, containsInAnyOrder(expected.toArray())); but no luck.

@Test
public void testGetOrderLines() {
    List<OrderLine> expResult = new ArrayList<>();
    OrderLine orderLine = new OrderLine(100, 5, "http://image-url.com", "Baby Gym",
            1, "physical", "http://product-url.com", 100, "pcs", "100-123");
    expResult.add(orderLine);
    List<OrderLine> result = instance.getOrderLines();
    assertThat(expResult, containsInAnyOrder(result.toArray()));
}

Error:

Failed tests:
  AuthorizationRequestTest.testGetOrderLines:92
Expected: iterable over [<com.ebayenterprise.payment.klarna.model.OrderLine@1710de08[totalAmount=100,totalDiscountAmount=5,imageUrl=http://image-url.com,name=Baby Gym,quantity=1,type=physical,productUrl=http://product-url.com,unitPrice=100,quantityUnit=pcs,reference=100-123]>] in any order
     but: Not matched: <com.ebayenterprise.payment.klarna.model.OrderLine@696911f6[totalAmount=100,totalDiscountAmount=5,imageUrl=http://image-url.com,name=Baby Gym,quantity=1,type=physical,productUrl=http://product-url.com,unitPrice=100,quantityUnit=pcs,reference=100-123]>

Aucun commentaire:

Enregistrer un commentaire