2014-03-29 2 views
0

에게 내가 가진 예외 메시지 오류를 사용하여AssertionError를 JUnit을

예상된다

[email protected]

데이터 형식이 같고 예외가 throw되는 이유를 알 수 없다고 생각합니다.

내 소스 코드는 다음과 같습니다

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++; 
    } 
+0

'(그리고 좋은 측정을 위해 : 당신이 이것을 구현하는 방법의 포괄적 인 개요를

는이 항목을 읽어'.hashCode()') 사용자 정의 클래스 'TransactionReportItem'. –

+0

감사합니다. 나 해보자 :) – ndnguyen

답변

2

당신은 .equals()을 구현해야한다 (그리고 좋은 측정을위한 : .hashCode()) 사용자 정의 클래스 TransactionReportItem에.

assertEquals()은 입력 한 두 객체가 동일하다는 것을 주장하기 위해 .equals() 메소드를 사용합니다. 그러나 표준 구현은 원하는 것이 아닙니다. 일반적으로 객체가 보유하는 값을 기반으로 동등성을 결정하려고합니다. 당신은 .equals()`구현해야

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