你可以使用JUnit来比较两个String ArrayLists吗?

huangapple go评论60阅读模式
英文:

Can you use JUnit to compare two String ArrayLists?

问题

例如,如果您有以下两个ArrayList:

ArrayList<String> arrLst1 = new ArrayList<String>();
arrLst1.add("Hello");
arrLst1.add("Goodbye");

ArrayList<String> arrLst2 = new ArrayList<String>();
arrLst2.add("Greetings");
arrLst2.add("See you soon");

如果我想要使用JUnit,我的猜测是使用类似以下的代码:

import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;

public class myTest {
  @Test
  public void firstTest() {
    assertArrayEquals("Error Message", arrLst1, arrLst2);
  }
}

然而,我遇到了一个问题,运行代码后它表示这两个值是相等的。查阅文档,我没有找到适用于两个String ArrayList的assertArrayEquals()方法。这实际上可行吗?

英文:

For example, if you have the following two ArrayLists:

ArrayList&lt;String&gt; arrLst1 = new ArrayList&lt;String&gt;();
arrLst1.add(&quot;Hello&quot;);
arrLst1.add(&quot;Goodbye&quot;);

ArrayList&lt;String&gt; arrLst2 = new ArrayList&lt;String&gt;();
arrLst2.add(&quot;Greetings&quot;);
arrLst2.add(&quot;See you soon&quot;);

If I wanted to use JUnit, my guess would be to use something like this:

import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;

public class myTest {
  @Test
  public void firstTest() {
    assertArrayEquals(&quot;Error Message&quot;, arrLst1, arrLst2);
  }
}

However, I'm having an issue where once I run the code, it states that the two values are equal. Looking at the documentation, I don't see anything for assertArrayEquals() for two String ArrayLists. Is this even something that is possible?

答案1

得分: 4

assertArrayEquals 用于数组,而 ArrayList&lt;T&gt; 不是数组。

JUnit5

使用 JUnit5 并且:

assertIterableEquals(expected, actual); // 来自 org.junit.jupiter.api.Assertions

将会断言 expectedactual 可迭代对象是深度相等的。


JUnit4

如果您使用的是 JUnit4,请升级到 JUnit5;然而,如果仍然使用4版本,通常情况下,您需要在您的通用类型参数类(List&lt;T&gt; 中的 T)中重写 .equals() 方法,之后您可以使用:

assertEquals(expected, actual);

注意,如果您的通用参数(列表中的元素类型)是 String,您不需要(实际上也不能)重写 .equals(),因为 String 已经完美地覆盖了它。

英文:

assertArrayEquals is for arrays, and ArrayList&lt;T&gt; is not an array.

JUnit5

Use JUnit5 and:

assertIterableEquals(expected, actual); //from org.junit.jupiter.api.Assertions

will assert that expected and actual iterables are deeply equal.


JUnit4

If you use JUnit4, upgrade to JUnit5; however, if still with 4, then, generally, you will need to override .equals() method in your Generic Type Argument class (T in List&lt;T&gt;), after which, you can use:

assertEquals(expected, actual);

Note, that if your generic argument (typed contained in your list) is String, you do not need (you cannot, actually) override .equals(), since String already overrides it perfectly fine.

答案2

得分: 2

你可以使用assertEquals,它有一个重载版本,接受两个Object参数,就像这样:

import static org.junit.Assert.*;


var a = List.of("hello");
var b = List.of("hello");

assertEquals(a, b); // 通过

var c = List.of("hello");
var d = List.of("world");

assertEquals(c, d); // 失败

在这种情况下,assertEquals方法使用List.equals()重写方法来判断两个列表是否包含相同的内容。

英文:

You can use assertEquals, which has an overload taking two Object arguments, like so:

import static org.junit.Assert.*;


var a = List.of(&quot;hello&quot;);
var b = List.of(&quot;hello&quot;);

assertEquals(a, b); // passes

var c = List.of(&quot;hello&quot;);
var d = List.of(&quot;world&quot;);

assertEquals(c, d); // fails

In this case, assertEquals method uses the .equals() overridden method of List to determine if both lists contain the same contents.

huangapple
  • 本文由 发表于 2020年10月2日 07:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64164249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定