Search code examples
javaunit-testingtestingtestcase

Best way to test Java Program


I wanted to test the below java program.

package com.arrays;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Find all pairs in an array of non-negative unique numbers whose sum is equal to k
 * For Example. {1,3,4,5,6} and k=9, then {3,6} and {4,5}
 *
 */
public class FindAllPairsIfSumToK {

    public List<Pair> findAllPairsWhoseSumIsK(int[] inputArray, int k) {

        Set<Integer> tempSet = new HashSet<>();
        List<Pair> pairs = new ArrayList<>();

        for(int i=0;i<inputArray.length;i++) {
            tempSet.add(inputArray[i]);
        }

        for(int i=0;i<inputArray.length;i++) {
            if((2*inputArray[i] != k) && tempSet.contains(k-inputArray[i])) {
                pairs.add(new Pair(inputArray[i], k-inputArray[i]));
            }
        }

        return pairs;
    }

    public static void main(String[] args) {

        FindAllPairsIfSumToK findAllPairsIfSumToK = new FindAllPairsIfSumToK();

        //Test 1
        int[] inputArray1 = {1, 3, 5, 7, 9, 11};
        List<Pair> output1 = findAllPairsIfSumToK.findAllPairsWhoseSumIsK(inputArray1, 10);
        assert (output1.size() == 2) ;

        //Test 2
        int[] inputArray2 = {1, 2, 5, 6, 12, 15, 16};
        List<Pair> output2 = findAllPairsIfSumToK.findAllPairsWhoseSumIsK(inputArray2, 17);
        assert (output2.size() == 3) ;
    }

    class Pair{
        int value1,value2;

        public Pair(int value1, int value2) {
            this.value1 = value1;
            this.value2 = value2;
        }
    }
}

This is how I'm trying to test the program

//Test 1
            int[] inputArray1 = {1, 3, 5, 7, 9, 11};
            List<Pair> output1 = findAllPairsIfSumToK.findAllPairsWhoseSumIsK(inputArray1, 10);
            assert (output1.size() == 2) ;

I Googled it, But most of them are telling about testing the web application. Is it a proper way to test the program? Or Could you please guide?


Solution

  • For java there is a Unit Testing Library called JUnit. Depending on your IDE, you will have to include it in a different way, but here are the basics. If using JUnit, you can write multiple test cases, which is preferred to having the test case in the main function, since you can organise the tests better.

    If you are using eclipse, to generate a new JUnit TestCase simply right click on the source folder and select: new -> other -> JUnitTestCase and then select a JUnit version.

    In a JUnitTestCase all tests are functions annotated with @Test and the results are checked with assertEqals, assertArrayEquals, assertNonNull, etc. If you create a new source folder (call it test or something similar) and create the the same package structure as in your program and create the tests in the same package as the class to be tested, you can access package-private and protected members, but the tests are still organized separately from the main program.

    Your test would look like this using JUnit:

    @Test
    public void Test_FindAllPairsIfSumToK(){
        FindAllPairsIfSumToK findAllPairsIfSumToK = new FindAllPairsIfSumToK();
        int[] inputArray1 = {1, 3, 5, 7, 9, 11};
        List<Pair> output1 = findAllPairsIfSumToK.findAllPairsWhoseSumIsK(inputArray1, 10);
        assertEquals (2, output1.size());
        //maybe check if the correct ones are output:
        output1.foreach(p -> assertEquals(10, p.value1 + p.value2));
    }
    

    Also you might want to declare the variables in pair as public final int, so that they can be accessed from outside the package and cannot be changed after the Pair object was constructed.