Reputation: 6600
I am learning JUnit and I am testing a simple method, I have a method which populate a list and return it. Although in unit test I provided the same list, the unit test failed.
Code
public List<Transaction> retrieveData() throws ParseException {
List<Transaction> expResult = new ArrayList();
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd").parse("2014-04-29T13:15:54");
Transaction tran = new Transaction("A", convertedDate,new BigDecimal("20.00"));
expResult.add(tran);
tran = new Transaction("B", convertedDate,new BigDecimal("20.00"));
expResult.add(tran);
tran = new Transaction("A", convertedDate,new BigDecimal("20.00"));
expResult.add(tran);
tran = new Transaction("B", convertedDate,new BigDecimal("20.00"));
expResult.add(tran);
return expResult;
}
JUnit
@Test
public void testRetrieveData() throws ParseException {
MyClass instance = new MyClass();
List<Transaction> expResult = new ArrayList();
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd").parse("2014-04-29T13:15:54");
Transaction tran = new Transaction("A", convertedDate,new BigDecimal("20.00"));
expResult.add(tran);
tran = new Transaction("B", convertedDate,new BigDecimal("20.00"));
expResult.add(tran);
tran = new Transaction("A", convertedDate,new BigDecimal("20.00"));
expResult.add(tran);
tran = new Transaction("B", convertedDate,new BigDecimal("20.00"));
expResult.add(tran);
List<Transaction> result = instance.retrieveData();
assertEquals(expResult, result);
}
The failed message and stacktrace are as following
Error
expected: java.util.ArrayList<[
Transaction{cardNumber=A, timestamp=Tue Apr 29 00:00:00 EST 2014, price=20.00},
Transaction{cardNumber=B, timestamp=Tue Apr 29 00:00:00 EST 2014, price=20.00},
Transaction{cardNumber=A, timestamp=Tue Apr 29 00:00:00 EST 2014, price=20.00},
Transaction{cardNumber=B, timestamp=Tue Apr 29 00:00:00 EST 2014, price=20.00}]>
but was: java.util.ArrayList<[
Transaction{cardNumber=A, timestamp=Tue Apr 29 00:00:00 EST 2014, price=20.00},
Transaction{cardNumber=B, timestamp=Tue Apr 29 00:00:00 EST 2014, price=20.00},
Transaction{cardNumber=A, timestamp=Tue Apr 29 00:00:00 EST 2014, price=20.00},
Transaction{cardNumber=B, timestamp=Tue Apr 29 00:00:00 EST 2014, price=20.00}]>
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:93)
at org.junit.Assert.failNotEquals(Assert.java:647)
at org.junit.Assert.assertEquals(Assert.java:128)
at org.junit.Assert.assertEquals(Assert.java:147)
Upvotes: 0
Views: 743
Reputation: 49
This is a discussion and thorough answer about a similar issue:
How can I check if two ArrayList differ, I don't care what's changed
Upvotes: 0
Reputation: 4462
When comparing the lists, your test is asserting the lists are equals, which will call equals()
on all of the elements within the list.
The default implementation of equals
for all classes simply asserts that the objects are literally the same (Same pointer).
If you override equals()
in your Transaction
class to check the values the class holds instead of the references, the test will behave as you expect.
Upvotes: 1