带有参数化测试的 Junit4
Junit4 with Parameterized Test
我有下面的 Junit 测试,我正在尝试 运行 作为参数化测试以允许使用测试方法测试多个条件,但是 运行 宁此
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class ProductCategoryTest {
private boolean isAggregate;
private ProductCategory category;
public ProductCategoryTest(boolean isAggregate, ProductCategory category) {
this.isAggregate = isAggregate;
this.category = category;
}
@Parameterized.Parameters
public static List<Object[]> categoryList() {
return Arrays.asList(new Object[][]{
{true, new ProductCategory("A", "Laundry", ProductCategory.Type.Aggregate)},
{false, new ProductCategory("B", "Dryer", ProductCategory.Type.Individual)},
});
}
@Test
public void isAggregateProductCategory() throws Exception {
assertEquals(isAggregate, category.isAggregateProductCategory());
}
}
我得到的异常:
java.lang.Exception: No tests found matching categoryList with any parameter from org.junit.runner.Request@123772c4
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:96)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
我在我的电脑上试过你的代码,它在我的电脑上运行正常。
我怀疑您的环境中的库或 JUnit 运行程序存在问题。
尝试输入您自己的测试运行程序。这是一个如何做到这一点的例子:
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.List;
import org.junit.runner.Result;
import static org.junit.Assert.assertEquals;
class ProductCategory {
enum Type {
Aggregate, Individual
};
public ProductCategory(String a, String b, Type t) {
}
}
@RunWith(Parameterized.class)
public class ProductCategoryTest {
private boolean isAggregate;
private ProductCategory category;
public ProductCategoryTest(boolean isAggregate, ProductCategory category) {
this.isAggregate = isAggregate;
this.category = category;
}
@Parameterized.Parameters
public static List<Object[]> categoryList() {
return Arrays.asList(new Object[][]{
{true, new ProductCategory("A", "Laundry", ProductCategory.Type.Aggregate)},
{false, new ProductCategory("B", "Dryer", ProductCategory.Type.Individual)},
});
}
@Test
public void isAggregateProductCategory() throws Exception {
//assertEquals(isAggregate, category.isAggregateProductCategory());
}
public static void main(String[] args) {
Result result = JUnitCore.runClasses(ProductCategoryTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
我有下面的 Junit 测试,我正在尝试 运行 作为参数化测试以允许使用测试方法测试多个条件,但是 运行 宁此
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class ProductCategoryTest {
private boolean isAggregate;
private ProductCategory category;
public ProductCategoryTest(boolean isAggregate, ProductCategory category) {
this.isAggregate = isAggregate;
this.category = category;
}
@Parameterized.Parameters
public static List<Object[]> categoryList() {
return Arrays.asList(new Object[][]{
{true, new ProductCategory("A", "Laundry", ProductCategory.Type.Aggregate)},
{false, new ProductCategory("B", "Dryer", ProductCategory.Type.Individual)},
});
}
@Test
public void isAggregateProductCategory() throws Exception {
assertEquals(isAggregate, category.isAggregateProductCategory());
}
}
我得到的异常:
java.lang.Exception: No tests found matching categoryList with any parameter from org.junit.runner.Request@123772c4
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:96)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
我在我的电脑上试过你的代码,它在我的电脑上运行正常。 我怀疑您的环境中的库或 JUnit 运行程序存在问题。
尝试输入您自己的测试运行程序。这是一个如何做到这一点的例子:
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.List;
import org.junit.runner.Result;
import static org.junit.Assert.assertEquals;
class ProductCategory {
enum Type {
Aggregate, Individual
};
public ProductCategory(String a, String b, Type t) {
}
}
@RunWith(Parameterized.class)
public class ProductCategoryTest {
private boolean isAggregate;
private ProductCategory category;
public ProductCategoryTest(boolean isAggregate, ProductCategory category) {
this.isAggregate = isAggregate;
this.category = category;
}
@Parameterized.Parameters
public static List<Object[]> categoryList() {
return Arrays.asList(new Object[][]{
{true, new ProductCategory("A", "Laundry", ProductCategory.Type.Aggregate)},
{false, new ProductCategory("B", "Dryer", ProductCategory.Type.Individual)},
});
}
@Test
public void isAggregateProductCategory() throws Exception {
//assertEquals(isAggregate, category.isAggregateProductCategory());
}
public static void main(String[] args) {
Result result = JUnitCore.runClasses(ProductCategoryTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}