user395817
user395817

Reputation: 305

Junit test on an ArrayList of ArrayLists

I am a beginner in Java I'm trying to learn by practice and I found this exercise that has as a purpose to partition a list to sublists of n size the method partition take in argument (ArrayList, size)

For example:
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;
    }
}

I want to write a Junit test to test all cases. I'm trying to read documentation on how to use Junit but I'm finding some difficulties to do it for this case. Can someone help me please by giving some indication or an example which looks like this one so I can test all the cases.

Upvotes: 2

Views: 1820

Answers (2)

davidxxx
davidxxx

Reputation: 131326

Your use case seems to be a good candidate for parameterized tests.
JUnit 5 is really mature now. So I encourage to use it.
The overall idea in unit tests is identifying scenarios : what you have as input and what you have as expected.
In your question, you successfully started to define them.
But a good unit test is a unit test that doesn't leave holes. So you also have to identify and test the corner cases.
You could for example define the scenarios (not exhaustive but gives you an idea) :

partition([1,2,3,4,],2) will return ([1,2],[3,4]) // nominal case
partition([1,2,3,4,],3) will return ([1,2,3],[4]) // nominal case
partition([1,2,3,4,],5) will return ()  // corner case
partition([],2) will return () // corner case

Now write a test for them :

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 {

    @ParameterizedTest
    @MethodSource("partitionFixture")
    void partition(List<Integer> originalList, int partitionSize, List<List<Integer>> expectedListOfList) {
        ArrayList<ArrayList<Integer>> actualListOfList = new Partition().partition(originalList, partitionSize);
        Assertions.assertEquals(expectedListOfList, actualListOfList);
    }

    @SuppressWarnings("unused")
    private static Stream<Arguments> partitionFixture() {
        return Stream.of(Arguments.of(asList(1, 2, 3, 4), 2, asList(asList(1, 2), asList(3, 4))),
                         Arguments.of(asList(1, 2, 3, 4), 3, asList(asList(1, 2, 3), asList(4))),
                         Arguments.of(asList(1, 2, 3, 4), 5, asList()),
                         Arguments.of(asList(), 2, asList()));
    }

}

Upvotes: 1

Emre Savcı
Emre Savcı

Reputation: 3070

So you are wrote your class and want to test it to see actually works. Here what you can do with unit test:

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class Tests {
    Partition sut = new Partition();

    @Test
    public void it_should_has_partition_size_2() {    
        ArrayList<ArrayList> partitioned = sut.partition(Arrays.asList(1,2,3,4),2);

        assertEquals(partitioned.size(), 2); // we expecting size to be 2
        assertEquals(partitioned.get(0), Arrays.asList(1,2,3)); // we know the first element should contains 1,2,3
    }

    @Test
    public void it_should_fail() {
        ArrayList<ArrayList> partitioned = main.partition(Arrays.asList(1,2,3,4),3);

        assertEquals(partitioned.size(), 2);
        // here it fails because we should expect partitioned.get(0) contains 1,2,3
        assertEquals(partitioned.get(0), Arrays.asList(1,2));
        /*
         * to pass this test we should use the below method
         * assertNotEquals(partitioned.get(0), Arrays.asList(1,2));
         */
    }
}

When you run it the first test will pass and second one is fails. And it gives an explanation about why second one is failed:

enter image description here

Maven dependency for junit:

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

Upvotes: 1

Related Questions