我在Junit中遇到参数化测试问题。我已经坚持了一段时间了,我想知道是否有人可以帮助我。
这是我的代码
@RunWith(Parameterized.class)
public class DomainTestWithinBorders {
int x;
float y;
boolean expectedOut;
void DomainTest(int xIn, int yIn, boolean out) {
this.x = xIn;
this.y = yIn;
expectedOut = out;
}
@Test
public void testEqual() {
boolean actualOut = (x == y);
assertEquals(expectedOut, actualOut);
}
@Parameters
public static Collection<Object[]> data() {
Object[][] values = { { 0, 10.0, false }, { 1, 16.0, false },
{ 17, 17.0, true } };
return Arrays.asList(values);
}
}
运行时,我收到以下错误:
java.lang.IllegalArgumentException: wrong number of arguments
我不知道为什么会收到此错误。我觉得我已经尝试了一切。
答案 0 :(得分:2)
首先,你的构造函数实际上不是构造函数:
void DomainTest(int xIn, int yIn, boolean out) {
this.x = xIn;
this.y = yIn;
expectedOut = out;
}
应该是:
public DomainTestWithinBorders(int xIn, float yIn, boolean out) {
this.x = xIn;
this.y = yIn;
this.expectedOut = out;
}
(请注意yIn
的正确类型为float
,而不是int
):
如果你解决了这个问题,你仍会得到以下例外:
java.lang.IllegalArgumentException:参数类型不匹配
要修复它,请更改:
Object[][] values = { { 0, 10.0, false }, { 1, 16.0, false },
{ 17, 17.0, true } };
为:
Object[][] values = { { 0, 10.0f, false }, { 1, 16.0f, false },
{ 17, 17.0f, true } };
答案 1 :(得分:1)
import static junit.framework.Assert.assertEquals;
import org.junit.runner.RunWith;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;
@RunWith(ZohhakRunner.class)
public class DomainTestWithinBorders {
@TestWith({
"0, 10.0, false",
"1, 16.0, false",
"17, 17.0, true"
})
public void testEqual(int x, float y, boolean expectedOut) {
boolean actualOut = (x == y);
assertEquals(expectedOut, actualOut);
}
}
答案 2 :(得分:0)
我更改了构造函数以及data()方法中的一些修改
@RunWith(Parameterized.class)
public class DomainTestWithinBorders extends TestCase {
int x;
float y;
boolean expectedOut;
public DomainTestWithinBorders(int xIn, float yIn, boolean out) {
this.x = xIn;
this.y = yIn;
expectedOut = out;
}
@Test
public void testEqual() {
boolean actualOut = (x == y);
assertEquals(expectedOut, actualOut);
}
@Parameters
public static Collection< Object[] > data() {
Object[][] values = { { 0, 10.0f, false } };
return Arrays.asList(values);
}
}