1

I am trying to do Parameterization in Junit but in my case its giving me errors. Here is the code that i got in a online tutorial.

import java.util.Arrays;
import java.util.Collection;
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 parametrization {


    public String username;
    public String password;
    public int pincode;


    public parametrization (String username, String password, int pincode){
        this.username=username;
        this.password=password;
        this.pincode=pincode;
    }

@Parameters
public static Collection<Object[]> getdata(){
    Object[][] data = new Object[2][3];  
    //row 1st
    data [0][0] = "usernamea";
    data [0][1] = "passa";
    data [0][2] = "111";

    //row 2nd
    data [1][0] = "usernameb";
    data [1][1] = "passb";
    data [1][2] = "222";

    return Arrays.asList(data);
    }


    @Test
    public void testlogin()
        {
        System.out.println(username + "--" + password + "--" + pincode);
        }
}

On execution of the above code, i got errors like

java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTestUsingConstructorInjection(Parameterized.java:186)
    at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTest(Parameterized.java:181)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Is there something that m missing??

4
  • Sorry, this makes no sense to me. What are you trying to accomplish? It seems like it's an easy problem that you've made difficult by introducing unnecessary complexity. Is this just a simplified example of what you really want to do? Commented Aug 1, 2013 at 12:07
  • Yes i am new to this. So started with simple code. Commented Aug 1, 2013 at 12:20
  • No, that's not simple. Commented Aug 1, 2013 at 12:23
  • So what should be a simple way for parametrization in junit + webdriver ?. Commented Aug 1, 2013 at 12:26

2 Answers 2

5

You issue is that the 3rd argument is an int and you are passing a String

public parametrization (String username, String password, int pincode){
    this.username=username;
    this.password=password;
    this.pincode=pincode;
}

Change this

@Parameters
public static Collection<Object[]> getdata(){
    Object[][] data = new Object[2][3];  
    //row 1st
    data [0][0] = "usernamea";
    data [0][1] = "passa";
    data [0][2] = "111";

to this

@Parameters
public static Collection<Object[]> getdata(){
    Object[][] data = new Object[2][3];  
    //row 1st
    data [0][0] = "usernamea";
    data [0][1] = "passa";
    data [0][2] = new Integer(111);
Sign up to request clarification or add additional context in comments.

Comments

0

or else you could simple remove the double qoutes ("")of int and it should work fine.

@Parameters public static collection getData(){

    Object obj[][] = new Object[4][3];

    obj[0][0] = "Kevin";
    obj[0][1] = "Anderson";
    obj[0][2] = 270123;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.