Java JUnit 测试失败但显示相同的预期和实际结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21700088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:09:34  来源:igfitidea点击:

JUnit test failing but showing identical Expected and Actual results

javaunit-testingintellij-ideajunit4

提问by nihilon

So, I don't even know where to start with this one. This whole fiasco began over eight hours ago, jerry rigging my way out of one issue after another. Now, this: Junit test failing, with passing resultsAnd it gets even better, after clicking "Click to see difference": enter image description here

所以,我什至不知道从哪里开始这个。整个惨败开始于八小时前,杰瑞在一个又一个的问题上操纵我的方式。现在,这个: Junit 测试失败,结果通过在点击“点击查看差异”后,它变得更好了: 在此处输入图片说明

I have:
- Invalidated caches and restarted IntelliJ
- Attempted running where Double[] numSet = {23.0};and then setting the expected result to numSetrather than "[23.0]". No dice.
- The answer regarding adding a delta didn't work: JUnit test fails even if the expected result is correct

我有:
- 缓存无效并重新启动 IntelliJ
- 尝试运行 whereDouble[] numSet = {23.0};然后将预期结果设置为numSet而不是"[23.0]". 没有骰子。
- 有关添加增量的答案不起作用:即使预期结果正确,JUnit 测试也会失败

I feel it is also worth noting that the error supposedly occuring at line 48 does not show up when calling the method from the main class, so I have no clue as to what that might be about.

我觉得还值得注意的是,当从主类调用该方法时,第 48 行的错误没有出现,所以我不知道这可能是什么原因。

Any help will be appreciated.

任何帮助将不胜感激。

Here is the code for the method being tested. Almost forgot it:

这是正在测试的方法的代码。差点忘了:

public List<Object> trimNumberSet(Double[] numSet) {
    List<Object> trimmedNumberSet = new ArrayList<>();

    for (int i = 0; i < numSet.length; i++) {
        if (numSet[i] != null) {
            trimmedNumberSet.add(numSet[i]);
        }
    }
    return trimmedNumberSet;
}

采纳答案by Alexis C.

You are comparing a Stringand a List<Object>.

您正在比较 aString和 a List<Object>

So you're using the method assertEquals(Object expected, Object actual);

所以你使用的方法 assertEquals(Object expected, Object actual);

Obviously a Stringis not equals to a List<Object>and hence the fail.

显然aString不等于a List<Object>,因此失败。

But since the String representation of the list is the same as you wrote, it hides you the result in the console because it uses this representation to show you the result of this test.

但是由于列表的字符串表示与您编写的相同,它会将结果隐藏在控制台中,因为它使用此表示向您显示此测试的结果。

So you should compare the String representation of boths :

所以你应该比较两者的字符串表示:

assertEquals("[23.0]", setSorteer.trimNumberSet(numSet).toString());

A better idea would be to compare two Lists directly, and not base your test on the String representations because they can change in the future and your test will fail :

一个更好的主意是直接比较两个列表,而不是基于字符串表示进行测试,因为它们将来可能会发生变化并且您的测试将失败:

assertEquals(Arrays.asList(23.0), setSorteer.trimNumberSet(numSet));



还有没有List<Double>List<Double>在方法中返回 a 的任何原因?

public List<Double> trimNumberSet(Double[] numSet) {
    List<Double> trimmedNumberSet = new ArrayList<>();
    ...
    return trimmedNumberSet;
}

回答by Francisco Hernandez

I think your problem came across the data types. Even when the data are the 'same', it is possible that the test is comparing String vs Integer (or whatever the number implementation being used is). This error would occur in other circumstances...

我认为您的问题遇到了数据类型。即使数据是“相同的”,测试也可能是在比较 String 与 Integer(或正在使用的任何数字实现)。在其他情况下会发生此错误...

Try to convert the left side to the same data type that setSorter.trimNumberSetretrieve or convert setSorter.trimNumberSet's return object to a String in assertEquals.

尝试将左侧setSorter.trimNumberSet转换setSorter.trimNumberSet为与在 assertEquals中检索或转换的返回对象相同的数据类型。

Hope that helps!

希望有帮助!

回答by spike

As ZouZou said, you are comparing different data types. numSet is an array of Doubles, whereas the result of trimNumberSet is a List of Doubles.

正如邹走所说,您正在比较不同的数据类型。numSet 是一个双精度数组,而 trimNumberSet 的结果是一个双精度列表。

In addition, note: When comparing the Double value in your List to a String, I highly recommend that you also use a number formatter to ensure that the scale is as you expect (i.e. "23.0" instead of, for example, "23.000001"). The internal representation of Double values will often cause the effect that what you get out is not precisely what you have put in before.

此外,请注意:将列表中的 Double 值与字符串进行比较时,我强烈建议您还使用数字格式化程序以确保比例符合您的预期(即“23.0”而不是“23.000001” )。Double 值的内部表示通常会导致您得到的结果与您之前放入的不完全相同。