[英]how could I test Lists in JUnit5?
我尝试测试2个ArraysLists时遇到错误。 似乎错误是在我的removeEndWith_at方法中说“toArray()未定义”。 你能给我一个如何测试这2个ArraysList的建议吗?
谢谢。
Java版本:jdk-10.0.2
JUnit的:5
[ArrayListIterator类]
import java.util.Iterator;
import java.util.List;
public class ArrayListIterator {
/**
* @param wordsAl : list of words
*/
public List<String> removeEndWith_at(List<String> wordsAl) {
Iterator<String> iterator = wordsAl.iterator();
while (iterator.hasNext()) {
if (iterator.next().endsWith("at"))
iterator.remove();
}
return wordsAl;
}
}
[ArrayListIteratorTest类]
import static org.junit.Assert.assertArrayEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
class ArrayListIteratorTest {
ArrayListIterator alIterator = new ArrayListIterator();
List<String> actualWords = Arrays.asList("Apple", "Bat", "Orange", "Cat");
@Test
void testremoveEndWith_at() {
actualWords = alIterator.removeEndWith_at(actualWords);
List<String> expectedvalue = Arrays.asList("Apple", "Orange");
assertArrayEquals(expectedvalue.toArray(), actualWords.toArray());
}
}
看着那(这
Arrays.asList()创建的List上的remove()抛出UnsupportedOperationException
Arrays.asList()
方法只是在原始元素周围创建一个包装器,并且在这个包装器上没有实现改变其大小的方法。
另请removeEndWith_at
我的方法removeEndWith_at
实现。 它比你的版本简单
/**
* @param wordsAl : list of words
*/
public List<String> removeEndWith_at(List<String> wordsAl) {
wordsAl.removeIf(s -> s.endsWith("at"));
return wordsAl;
}
使用Jupiter Assertion API比较List<String>
两个实例时,请尝试使用assertLinesMatch 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.