Java 使用JUnit时的AssertionError
我得到的异常消息错误是 预计:sg.team7ft.model。TransactionReportItem@2c2dc991但是是:sg.team7ft.model。TransactionReportItem@720c653f 我认为数据类型是相同的,我不知道为什么它会抛出异常 我的源代码是:Java 使用JUnit时的AssertionError,java,junit,Java,Junit,我得到的异常消息错误是 预计:sg.team7ft.model。TransactionReportItem@2c2dc991但是是:sg.team7ft.model。TransactionReportItem@720c653f 我认为数据类型是相同的,我不知道为什么它会抛出异常 我的源代码是: ArrayList<TransactionReportItem> tranItemReport = new ArrayList<TransactionReportItem>
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++;
}
ArrayList tranItemReport=new ArrayList();
tranItemReport.add(新交易报告项目(“NUS记事本”,“这些讲座的好记事本”,“Annand B”,1,“2014年4月21日”);
tranItemReport.add(新交易报告项目(“百周年跳楼”、“一个非常美好的时刻”、“Danial B”,1,“2014年4月21日”);
ArrayList tranItemReportTests=report.generateTransactionReport(开始日期,结束日期);
迭代器tranReportI=tranItemReportTests.Iterator();
int i=0;
while(tranReportI.hasNext())
{
TransactionReportItem tranReportTe=tranReportI.next();
//assertEquals(transReportte.getQuantity(),transItemReport.get(i.getQuantity());
尝试
{
assertEquals(transreportte,transitemreport.get(i));
}
捕获(断言错误)
{
字符串msg=e.getMessage();
}
i++;
}
您必须在自定义类TransactionReportItem
中实现.equals()
(并且为了更好地衡量:.hashCode()
)
assertEquals()
将使用该方法断言您提供的两个对象是相同的。但是,标准实现并不是您想要的:通常您希望根据对象所持有的值来确定相等性
要全面了解应如何实现此功能,请通读本主题:
您必须在自定义类
TransactionReportItem
中实现.equals()
(为了更好地衡量:.hashCode()
),谢谢。让我试试:)