Search code examples
javajunitjunit3

How to do the jUnit testing


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.


Solution

  • 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);
        }
    
    }