我正在尝试在Junit中进行参数化,但就我而言,它给了我错误。 这是我在在线教程中获得的代码。
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);
}
}
执行上面的代码时,我遇到了错误,如
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)
有什么东西可以丢失吗?
答案 0 :(得分:5)
问题是,第3个参数是int
,而您传递的是String
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";
到这个
@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);
答案 1 :(得分:0)
或者你可以简单地删除int的double qoutes(“”),它应该可以正常工作。
@参数 public static collection getData(){
Object obj[][] = new Object[4][3];
obj[0][0] = "Kevin";
obj[0][1] = "Anderson";
obj[0][2] = 270123;