JUnit Testing:while writing parameterized JUnit test cases to swap two numbers,it shows the following error

208 Views Asked by At

Error shown:

java.lang.IllegalArgumentException: wrong number of arguments: 4 expected: 2.

In my source program, I have given an array as input and array as output. I have difficulty in writing parameterized JUnit test cases. I am using JUnit4.

Here is my source code.

package BasicTesting;

import java.io.*;
import java.lang.reflect.Array;
import java.util.Scanner;
public class Swap_Array
{
  public static void main(String args[])throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int i;
    
      int [] array=new int[2];
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the two numbers");
      for(i=0;i<2;i++) {
          array[i] =sc.nextInt();
          
          
      }
      int [] p = swap(array);
      System.out.println("after swapping first number"+" "+p[0]+" "+" and second number are"+" " +p[1]);
    }



public static int[] swap(int array[]) {
     int[] c=new int[2];
     int i = array[0];
     int j = array[1];
      
      
    i=i+j;
    j=i-j;
    i=i-j;
    c[0]=i;
    c[1]=j;
    return c;
    
    
  }
 
}

Here is my Parameterized JUnit test case.

package BasicTesting;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)

public class Swap_Parameterized_testcase {
    

    public Swap_Parameterized_testcase(int[] input,  int[] expected) {
        super();
        this.input= input;
    
        this.expected = expected;
    }

    Swap_Array ob=new Swap_Array();
    
    
    private int input[];
    
    private int [] expected;
    @Parameters
    
    public static Collection<Object[]> testPrime() {
        return Arrays.asList(new Object[][]{{1,2,2,1},{23,44,44,23}});
        
    }
    
    
    
    @Test
    public void Swap_test() {
        assertArrayEquals(expected,ob.swap(input));
    }

}

How to rewrite this JUnit Parameterized test cases into the correct one?

How can we write JUnit parameterized test cases, If we give input to swap two numbers as two numbers instead of an array? Here is my source code, if the input is given as two numbers instead of an array.

package BasicTesting;

import java.io.*;
import java.lang.reflect.Array;
import java.util.Scanner;
public class Swap_Numbers
{
  public static void main(String args[])throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int i,j;
    System.out.println("enter first number..");
    i=Integer.parseInt(br.readLine());
    System.out.println("enter second number..");
    j=Integer.parseInt(br.readLine());
    int[] p=swap1(i,j);
    
      System.out.println("after swapping first number"+" "+p[0]+" "+" and second number are"+" " +p[1]);
    }



public static int[] swap1(int i,int j) {
     int[] c=new int[2];
    
      
    i=i+j;
    j=i-j;
    i=i-j;
    c[0]=i;
    c[1]=j;
    return c;
    
    
  }
 
}

Kindly help me to rewrite JUnit parameterized test cases for both. It would be more helpful you rewrite this code.

1

There are 1 best solutions below

0
duffymo On

I'd urge you to upgrade from Junit 4.x to 5.x. The way they do parameterized tests is slightly different.

The runtime tells you the offending line number, but you don't tell us in your question. That information would help a great deal.

Believe the JVM. Look at the line it cites and fix it.