猿问

如何在 JUnit5 中测试列表?

当我尝试测试 2 个 ArraysLists 时出现错误。似乎错误是在我的 removeEndWith_at 方法中说“toArray() 未定义”。你们能给我一个建议如何测试这两个 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());

    }


}


天涯尽头无女友
浏览 84回答 2
2回答

拉丁的传说

当我尝试测试 2 个 ArraysLists 时出现错误。似乎错误是在我的 removeEndWith_at 方法中说“toArray() 未定义”。你们能给我一个建议如何测试这两个 ArraysList 吗?谢谢。Java 版本:jdk-10.0.2JUnit:5[ArrayListIterator 类]import java.util.Iterator;import java.util.List;public class ArrayListIterator {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param wordsAl : list of words&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public List<String> removeEndWith_at(List<String> wordsAl) {&nbsp; &nbsp; &nbsp; &nbsp; Iterator<String> iterator = wordsAl.iterator();&nbsp; &nbsp; &nbsp; &nbsp; while (iterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (iterator.next().endsWith("at"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iterator.remove();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return wordsAl;&nbsp; &nbsp; }}[ArrayListIteratorTest 类]import static org.junit.Assert.assertArrayEquals;import java.util.Arrays;import java.util.List;import org.junit.jupiter.api.Test;class ArrayListIteratorTest {&nbsp; &nbsp; ArrayListIterator alIterator = new ArrayListIterator();&nbsp; &nbsp; List<String> actualWords = Arrays.asList("Apple", "Bat", "Orange", "Cat");&nbsp; &nbsp; @Test&nbsp; &nbsp; void testremoveEndWith_at() {&nbsp; &nbsp; &nbsp; &nbsp; actualWords = alIterator.removeEndWith_at(actualWords);&nbsp; &nbsp; &nbsp; &nbsp; List<String> expectedvalue = Arrays.asList("Apple", "Orange");&nbsp; &nbsp; &nbsp; &nbsp; assertArrayEquals(expectedvalue.toArray(), actualWords.toArray());&nbsp; &nbsp; }}看着那(这Arrays.asList() 创建的 List 上的 remove() 抛出 UnsupportedOperationExceptionArrays.asList()方法只是在原始元素周围创建一个包装器,并且在这个包装器上没有实现改变其大小的方法。另请查看我的方法实现removeEndWith_at。它比你的版本简单&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param wordsAl : list of words&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public List<String> removeEndWith_at(List<String> wordsAl) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wordsAl.removeIf(s -> s.endsWith("at"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return wordsAl;&nbsp; &nbsp; }

梦里花落0921

在比较List<String>使用 Jupiter Assertion API 的两个实例时,试试assertLinesMatch。
随时随地看视频慕课网APP

相关分类

Java
我要回答