2012-11-26 50 views
1

我想比較兩個測試列表。 我有這個代碼如何檢查通用列表與jUnit是否相等?

@Test 
public void testGetVerticalCells() 
{ 
    int[][] m = new int[][] 
      { 
       { 1, 0, 1 }, 
       { 1, 1, 0 }, 
       { 0, 1, 1 } 
      }; 
    int i = 1; 
    int j = 2; 
    Point sPoint = new Point(i, j); 
    potentialAlgorithm.startPoint = sPoint; 
    List<Point> actuals = potentialAlgorithm.getVerticalCells(m, i, j); 
    List<Point> expecteds = new ArrayList<Point>() { 
     { 
      add(new Point(0, 2)); 
      add(new Point(2, 2)); 
     } 
    }; 
    assertTrue(actuals.containsAll(expecteds)); 
} 

我使用jUnit 4.11。

我找回這個錯誤

java.lang.AssertionError 
    at org.junit.Assert.fail(Assert.java:86) 
    at org.junit.Assert.assertTrue(Assert.java:41) 
    at org.junit.Assert.assertTrue(Assert.java:52) 
    at org.vdzundza.transportation_problem.PotentialAlgorithmTest.testGetVerticalCells(PotentialAlgorithmTest.java:209) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

更新

public boolean equals(Point b) { 
    if(this.i == b.i && this.j == b.j) return true; 
    else return false; 
} 

如何解決這個問題呢?

+4

,你能否告訴我們'Point.equals(對象)'執行?除非這是正確的,否則它將不起作用。 –

+1

您可以在斷言前打印出「actual」和「expected」的內容嗎? – Marvo

+0

@PeterLawrey謝謝。我知道我的錯誤在哪裏。 – BILL

回答

2

的soultion比一個想象的更簡單:

assertTrue(actuals.equals(expected)); 
+0

如果您發現我的答案有用,您可以接受它(複選框) – AlexWien

+1

更簡單的是'assertEquals(expected,actual)'如果失敗,這也會給出更有意義的錯誤消息。 –