为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 JUnit5 中测试列表?

如何在 JUnit5 中测试列表?

天涯尽头无女友 2022-11-10 16:25:36
当我尝试测试 2 个 ArraysLists 时出现错误。似乎错误是在我的 removeEndWith_at 方法中说“toArray() 未定义”。你们能给我一个建议如何测试这两个 ArraysList 吗?谢谢。Java 版本:jdk-10.0.2JUnit: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());    }}
查看完整描述

2 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

当我尝试测试 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());

    }


}看着那(这


Arrays.asList() 创建的 List 上的 remove() 抛出 UnsupportedOperationException


Arrays.asList()

方法只是在原始元素周围创建一个包装器,并且在这个包装器上没有实现改变其大小的方法。


另请查看我的方法实现removeEndWith_at。它比你的版本简单


        /**

     * @param wordsAl : list of words

     */

    public List<String> removeEndWith_at(List<String> wordsAl) {

            wordsAl.removeIf(s -> s.endsWith("at"));

            return wordsAl;

    }


查看完整回答
反对 回复 2022-11-10
?
梦里花落0921

TA贡献1772条经验 获得超5个赞

在比较List<String>使用 Jupiter Assertion API 的两个实例时,试试assertLinesMatch



查看完整回答
反对 回复 2022-11-10
  • 2 回答
  • 0 关注
  • 71 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信