如何检查通用列表是否与jUnit相等?

时间:2012-11-26 17:06:49

标签: java list junit4

我想在测试中比较两个列表。 我有这个代码

@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;
}

如何解决此问题?

1 个答案:

答案 0 :(得分:2)

解决方案比一件事简单: