英文:
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<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");
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("Error Message", 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<T>
不是数组。
JUnit5
使用 JUnit5 并且:
assertIterableEquals(expected, actual); // 来自 org.junit.jupiter.api.Assertions
将会断言 expected
和 actual
可迭代对象是深度相等的。
JUnit4
如果您使用的是 JUnit4,请升级到 JUnit5;然而,如果仍然使用4版本,通常情况下,您需要在您的通用类型参数类(List<T>
中的 T
)中重写 .equals()
方法,之后您可以使用:
assertEquals(expected, actual);
注意,如果您的通用参数(列表中的元素类型)是 String
,您不需要(实际上也不能)重写 .equals()
,因为 String
已经完美地覆盖了它。
英文:
assertArrayEquals
is for arrays, and ArrayList<T>
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<T>
), 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("hello");
var b = List.of("hello");
assertEquals(a, b); // passes
var c = List.of("hello");
var d = List.of("world");
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论