Java junit.framework.AssertionFailedError 在 String[] 上执行 assertEquals 时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19041311/
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-12 13:36:31  来源:igfitidea点击:

junit.framework.AssertionFailedError while doing assertEquals on String[]

javajunitassert

提问by Alagappan Ramu

I have the following class which returns a list of strings.

我有以下类,它返回一个字符串列表。

public static String[] parseLinks(String text) {
    String[] result = new String[] {"",""};
            return result;
    }

But when I do an assertEquals on the result,

但是当我对结果执行 assertEquals 时,

    assertEquals(new String[]{"",""}, parseLinks(""));

I get the following error:

我收到以下错误:

Exception in thread "main" junit.framework.AssertionFailedError: expected: <[Ljava.lang.String;@2352544e> but was:<[Ljava.lang.String;@721cdeff>
at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.Assert.failNotEquals(Assert.java:329)
at junit.framework.Assert.assertEquals(Assert.java:78)
at junit.framework.Assert.assertEquals(Assert.java:86)
at junit.framework.TestCase.assertEquals(TestCase.java:253)

How do I go about fixing this? Please let me know if there is any other additional information that I have to provide.

我该如何解决这个问题?如果我必须提供任何其他额外信息,请告诉我。

采纳答案by Jeff Storey

You're comparing two different Array objects. You can use assertArrayEqualsto compare arrays instead.

您正在比较两个不同的 Array 对象。您可以使用assertArrayEquals来比较数组。