0

The message error in exception that I got is

expected:sg.team7ft.model.TransactionReportItem@2c2dc991 but was:sg.team7ft.model.TransactionReportItem@720c653f

I think that the data type is the same, and I dont know why it throws exception.

My source code is:

    ArrayList<TransactionReportItem> tranItemReport = new ArrayList<TransactionReportItem>();   
    tranItemReport.add(new TransactionReportItem("NUS Notepad", "Great Notepad for those lectures", "Annand B", 1, "21/04/2014"));
    tranItemReport.add(new TransactionReportItem("Centenary Jumper", "A really nice momento", "Danial B", 1, "21/04/2014"));

    ArrayList<TransactionReportItem> tranItemReportTests = report.generateTransactionReport(startDate, endDate);

    Iterator<TransactionReportItem> tranReportI = tranItemReportTests.iterator();
    int i = 0;
    while(tranReportI.hasNext())
    {
        TransactionReportItem tranReportTe = tranReportI.next();
    //  assertEquals(tranReportTe.getQuantity(), tranItemReport.get(i).getQuantity());
        try
        {
            assertEquals(tranReportTe, tranItemReport.get(i));
        }
        catch(AssertionError e)
        {
            String msg = e.getMessage();
        }
        i++;
    }
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
ndnguyen
  • 140
  • 1
  • 11

1 Answers1

2

You have to implement .equals() (and for good measure: .hashCode()) in your custom class TransactionReportItem.

assertEquals() will use that .equals() method to assert that the two objects you provided are the same. However, the standard implementation is not what you want: usually you want to determine equality based on the values that your object holds.

For a comprehensive overview of how you should implement this, read through this topic:

What issues should be considered when overriding equals and hashCode in Java?

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170