对 ArrayList 的 ArrayList 进行 Junit 测试

我是 Java 的初学者,我正在尝试通过实践来学习,我发现这个练习的目的是将列表分区为 n 大小的子列表,方法分区采用参数 (ArrayList, size)


例如:

partition([1,2,3,4,],2)will return ([1,2],[3,4])

partition([1,2,3,4,],3)will return([1,2,3],[4])


package partition;

import java.util.ArrayList;

import java.util.List;


public class Partition {



    public ArrayList<ArrayList> partition(List<Integer> li, int n) {

        ArrayList<ArrayList> al = new ArrayList();

        int start = 0;

        int i=n;

        for(; i<li.size(); i+=n){

            List<Integer> lis = li.subList(start, i);

            ArrayList<Integer> list = new ArrayList<>();

            list.addAll(lis);

            al.add(list);

            start = i;

        }


        if(i >= li.size()){

            List<Integer> lis = li.subList(start, li.size());

            ArrayList<Integer> list = new ArrayList<>();

            list.addAll(lis);

            al.add(list);

        }

        return al;

    }

}

我想编写一个 Junit 测试来测试所有情况。我正在尝试阅读有关如何使用 Junit 的文档,但我发现在这种情况下很难做到这一点。有人可以通过给出一些指示或一个看起来像这样的例子来帮助我,这样我就可以测试所有案例。


哔哔one
浏览 364回答 2
2回答

慕斯王

您的用例似乎是参数化测试的一个很好的候选者。JUnit 5 现在真的很成熟。所以我鼓励使用它。单元测试的总体思路是识别场景:你有什么作为输入,你有什么作为预期。在您的问题中,您成功地开始定义它们。但是一个好的单元测试是一个不会留下漏洞的单元测试。因此,您还必须识别和测试极端情况。例如,您可以定义场景(并非详尽无遗,但会给您一个想法):partition([1,2,3,4,],2) will return ([1,2],[3,4]) // nominal casepartition([1,2,3,4,],3) will return ([1,2,3],[4]) // nominal casepartition([1,2,3,4,],5) will return ()&nbsp; // corner casepartition([],2) will return () // corner case现在为他们写一个测试:import static java.util.Arrays.asList;import java.util.ArrayList;import java.util.List;import java.util.stream.Stream;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.Arguments;import org.junit.jupiter.params.provider.MethodSource;public class PartitionTest {&nbsp; &nbsp; @ParameterizedTest&nbsp; &nbsp; @MethodSource("partitionFixture")&nbsp; &nbsp; void partition(List<Integer> originalList, int partitionSize, List<List<Integer>> expectedListOfList) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<ArrayList<Integer>> actualListOfList = new Partition().partition(originalList, partitionSize);&nbsp; &nbsp; &nbsp; &nbsp; Assertions.assertEquals(expectedListOfList, actualListOfList);&nbsp; &nbsp; }&nbsp; &nbsp; @SuppressWarnings("unused")&nbsp; &nbsp; private static Stream<Arguments> partitionFixture() {&nbsp; &nbsp; &nbsp; &nbsp; return Stream.of(Arguments.of(asList(1, 2, 3, 4), 2, asList(asList(1, 2), asList(3, 4))),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Arguments.of(asList(1, 2, 3, 4), 3, asList(asList(1, 2, 3), asList(4))),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Arguments.of(asList(1, 2, 3, 4), 5, asList()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Arguments.of(asList(), 2, asList()));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java