这是我与JUnit的第一天。我尝试用参数进行测试。我有代码:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
@RunWith(JUnitParamsRunner.class)
public class MoneyParameterizedTest {
private static final Object[] getMoney() {
return new Object[] {
new Object[] {10, "USD"},
new Object[] {20, "EUR"}
};
}
@Test
@Parameterized.Parameters(method = "getMoney")
public void constructorShouldSetAmountAndCurrency(
int amount, String currency) {
Money money = new Money(amount, currency);
assertEquals(amount, money.getAmount());
assertEquals(currency, money.getCurrency());
}
}
IntelliJ告诉我:无法解析符号JUnitParamsRunner和方法。导入有问题吗?我正在测试的课程在同一个包中。
----- 编辑 -------
我将JunitParamsrunner.class更改为Parameterized.class并且没关系,但是Parametrized.Parameters中符号'method'的问题是相同的。
答案 0 :(得分:3)
JUnitParams
(licensed Apache 2.0)是一个单独的库,这意味着它不随JUnit本身一起提供。这也意味着您需要确保它位于项目的类路径中。如果您正在使用maven(或类似的东西),那么它很容易,您只需要将它作为依赖项添加到您的pom中并确保IJ已经接收到更改(如果它显示弹出窗口,则会自动或手动关闭右上角):
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.0.5</version>
<scope>test</scope>
</dependency>
否则,download it yourself(点击Download ( JAR )
链接)和library它到您的类路径。
请注意,虽然概念类似,但Parameterized
与JUnitParams
不同,后者试图简化和改进编写参数化JUnit测试的方式。
PS:还有另一个名为Zohhak
的库,它似乎比JUnitParams
更灵活,但它在LGPL 3.0
下发布,因此取决于您的许可限制。< / p>
答案 1 :(得分:1)
这是一个应该有效的例子:
import junitparams.JUnitParamsRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
//import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertEquals;
import junitparams.Parameters;
@RunWith(JUnitParamsRunner.class)
public class MoneyTest {
private final static int VALID_AMOUNT = 5;
private final static String VALID_CURRENCY = "USD";
private static final Object[] getInvalidAmount() {
return new Integer[][] {{-12387}, {-5}, {-1}};
}
@Test(expected = IllegalArgumentException.class)
@Parameters(method = "getInvalidAmount")
public void constructorShouldThrowIAEForInvalidAmount(int invalidAmount){
new Money(invalidAmount, VALID_CURRENCY);
}
private static final Object[] getInvalidCurrency(){
return new String[][]{{null}, {""}};
}
@Test(expected = IllegalArgumentException.class)
@Parameters(method = "getInvalidCurrency")
public void constructorShouldThrowIAEForInvalidCurrency(String invalidCurrency){
new Money(VALID_AMOUNT, invalidCurrency);
}
private static final Object[] getMoney() {
return new Object[] {
new Object[] {10, "USD"},
new Object[] {20, "EUR"}
};
}
@Test
@Parameters(method = "getMoney")
public void constructorShouldSetAmountAndCurrency(
int amount, String currency) {
Money money = new Money(amount, currency);
assertEquals(amount, money.getAmount());
assertEquals(currency, money.getCurrency());
}
}
答案 2 :(得分:0)
您的POM.xml应该喜欢这个...
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<groupId>groupId</groupId>
<artifactId>JUnit</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.0.5</version>
<scope>compile</scope>
</dependency>
</dependencies>