gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:35:44 24 4
gpt4 key购买 nike

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

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

如何解决这个问题?

最佳答案

解决方案比想象的要简单:

assertTrue(actuals.equals(expected));

关于java - 如何检查通用列表与 jUnit 是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13569643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com