How to do the jUnit testing

2k views Asked by At

I am brand new to jUnit testing and I do not know how it works, I tried to go through some tutorials online but couldn't understand much. So I am posting my code and the skeleton of the testClass, so how would the testClass be modified in this case, so that I can understand how jUnit works.

the method that I want to test:

public String quickSortArray(String numbers, String indexNumber) throws NumberFormatException, Exception{

                String[] data = numbers.split(";");
                int index = parseInt(indexNumber);
                int[] inputNum = new int[data.length];

                for (int i = 0; i < data.length; i++) {
                    inputNum[i] = parseInt(data[i]);

                }
                Set<Integer> removeDuplicates = new HashSet<Integer>();
                for (int j = 0; j < inputNum.length; j++) {
                    removeDuplicates.add(inputNum[j]);
                }
                int[] finalSortArray = new int[removeDuplicates.size()];
                int k = 0;
                for (Integer move : removeDuplicates) {
                    finalSortArray[k++] = move;
                }
                Arrays.sort(finalSortArray);
                if (finalSortArray.length < index) {
                    return "The index cannot be greater than the non-repeated numbers";                    
                } else {
                    int result=finalSortArray[finalSortArray.length-index];                    
                    return String.valueOf(result);
                }
            } 

The testClass generated by the IDE:

/**
     * Test of quickSortArray method, of class quickSort.
     */
    public void testQuickSortArray() throws Exception {
        System.out.println("quickSortArray");
        String numbers = "";
        String indexNumber = "";
        quickSort instance = new quickSort();
        String expResult = "";
        String result = instance.quickSortArray(numbers, indexNumber);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }

So I just want to know how the testClass should be written that my method is tested for its functionality. I would really appreciate all the help

What the method basically does is from an array of numbers separated by semicolon it first converts them to int and then removes the duplicates and then finds the Nth largest number. N is the index in this case.

Here is some manual data, I dont want the person answering to go through the trouble of calculating this as well: expResult=57; numbers="12;57;65" index="2";

expResult=39; numbers="09;78;45;39;05" index="3";

I just want to know how to use this in the code.

3

There are 3 answers

0
AudioBubble On BEST ANSWER

Supposing that your class looks like this

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;


public class X {

    public String quickSortArray(String numbers, String indexNumber) throws NumberFormatException, Exception{

        String[] data = numbers.split(";");
        int index = parseInt(indexNumber);
        int[] inputNum = new int[data.length];

        for (int i = 0; i < data.length; i++) {
            inputNum[i] = parseInt(data[i]);

        }
        Set<Integer> removeDuplicates = new HashSet<Integer>();
        for (int j = 0; j < inputNum.length; j++) {
            removeDuplicates.add(inputNum[j]);
        }
        int[] finalSortArray = new int[removeDuplicates.size()];
        int k = 0;
        for (Integer move : removeDuplicates) {
            finalSortArray[k++] = move;
        }
        Arrays.sort(finalSortArray);
        if (finalSortArray.length < index) {
            return "The index cannot be greater than the non-repeated numbers";                    
        } else {
            int result=finalSortArray[finalSortArray.length-index];                    
            return String.valueOf(result);
        }
    }

    private int parseInt(String indexNumber) {
        return Integer.parseInt(indexNumber);
    } 
}

I think you could write some JUnit test like this

import static org.junit.Assert.*;

import org.junit.Test;


public class XTest {

    @Test
    public void testQuickSortArray1() throws NumberFormatException, Exception {
        String numbers="12;57;65";
        String index="2";
        String result = new X().quickSortArray(numbers, index);
        assertEquals("57",result);
    }

    @Test
    public void testQuickSortArray2() throws NumberFormatException, Exception {
        String numbers="09;78;45;39;05";
        String index="3";
        String result = new X().quickSortArray(numbers, index);
        assertEquals("39",result);
    }

}
3
Oleg Sklyar On
  1. Split your method into smaller testable methods: here you have a combination of input parsing (from string to some sort of collection) and probably sorting (which is difficult to follow though) -- you want to test those functionalities separately
  2. Define "test cases" that you want to test, first not programmatically, but conceptually. For example you want to test if the method does not fail for empty data, works fine on sorted data, on data of size 1, of odd and even size, on a number of repeated values etc
  3. Define scenarios where the method should throw an exception and write test method for that annotating them as @Test(expected=IllegalArgumentException.class)
  4. Per test case implement a test method in the following form (this particular piece of code may not be compilable :)

    @Test
    public testPresortedValues()
    {
        List<Integer> data = Lists.newArrayList(1, 1, 1, 2, 25, 30);
        List<Integer> expected = Lists.newArrayList(1, 1, 1, 2, 25, 30);
        Sorter sorter = new Sorter();
        sorter.sort(data);
        for (int i = 0; i < data.size(); ++i)
        {
             Asser.assertEquals(data.get(i), expected.get(i);
        }
    }
    
4
ptyx On

What the method basically does is from an array of numbers separated by semicolon it first converts them to int and then removes the duplicates and then finds the Nth largest number. N is the index in this case

First step would be to split your method into 3 (or more) so you can test each aspect individually.

The idea of unit tests is that the assembly works if each individual brick works.

Your test then become much simpler:

@Test
public void parsesSimpleArray(){
    assertThat( parser.parse("12;57;65") ).eq(new int[]{12,57,65});
}

@Test
public void parseIgnoresEmptyStrings(){
    // or whatever your desired behavior should be
    assertThat( parser.parse("12;;65") ).eq(new int[]{12,65});
}

etc...