在参数化 jUnit 4 测试中访问参数 Collection<> 的问题
Problems with accessing parameter Collection<> in parameterized jUnit 4 testing
import org.junit.Test;
import java.util.*;
import java.io.*;
import java.math.BigDecimal;
import static org.junit.Assert.assertEquals;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class FibonacciTest {
private static final String FILENAME = "###############";
@Parameters
public static Collection<Object[]> data() {
Scanner s = null;
String line;
String[] values;
Collection<Object[]> check = Arrays.asList(new Object[][]{{0, new BigDecimal(0)}, {1, new BigDecimal(1)},{2, new BigDecimal(1)}});
try {
s = new Scanner(new File(FILENAME));
while (s.hasNextLine()){
int i = 3;
line = s.nextLine();
values = line.split("\s+");
((List<Object[]>) check).add(i, new Object[][]{{0, new BigDecimal(Integer.parseInt(values[1]))}});
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int random = (int) (Math.random() * 30);
return check;
}
private int fInput;
private BigDecimal fExpected;
public FibonacciTest(int input, BigDecimal expected) {
this.fInput = input;
this.fExpected = expected;
}
@Test
public void test() {
assertEquals(fExpected, Fibonacci.fib(fInput));
}
}
你好,我真的是 JUnit 测试的新手,今天做了很多研究。通过自我、尝试和箭头取得了很多成就,但我一直遇到一个问题。我正在尝试为动态斐波那契计算编写一个随机测试。测试是 运行 在集合检查中手动添加的基本案例。他们在工作。我想添加更多案例,它们由 .txt
文件控制。输入也有效,但我无法稍后将对象添加到我的集合中。这是我的方法,但它在第 30 行抛出 java.lang.UnsupportedOperationExeption:
((List<Object[]>) check).add(i, new Object[][]{{0, new BigDecimal(Integer.parseInt(values[1]))}});
转换由 IntelliJ 完成。
尝试了很多不同的集合和列表可能性,但我被困在这里。
谢谢你帮我。
您用 Arrays.asList()
创建了 check
。 List
是基于输入数组的,所以不能扩展。
一种常见的方法是将该列表复制到 ArrayList
,这将使其可扩展。
是这样的:
Collection<Object[]> check = Arrays.asList(new Object[][]{{0, new BigDecimal(0)}, {1, new BigDecimal(1)},{2, new BigDecimal(1)}});
check = new ArrayList<Object[]>( check ); // copy to mutable ArrayList
为避免 ugly/uncessary 类型转换,您可以检查 List
类型。 List
扩展 Collection
所以它是兼容的。
import org.junit.Test;
import java.util.*;
import java.io.*;
import java.math.BigDecimal;
import static org.junit.Assert.assertEquals;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class FibonacciTest {
private static final String FILENAME = "###############";
@Parameters
public static Collection<Object[]> data() {
Scanner s = null;
String line;
String[] values;
Collection<Object[]> check = Arrays.asList(new Object[][]{{0, new BigDecimal(0)}, {1, new BigDecimal(1)},{2, new BigDecimal(1)}});
try {
s = new Scanner(new File(FILENAME));
while (s.hasNextLine()){
int i = 3;
line = s.nextLine();
values = line.split("\s+");
((List<Object[]>) check).add(i, new Object[][]{{0, new BigDecimal(Integer.parseInt(values[1]))}});
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int random = (int) (Math.random() * 30);
return check;
}
private int fInput;
private BigDecimal fExpected;
public FibonacciTest(int input, BigDecimal expected) {
this.fInput = input;
this.fExpected = expected;
}
@Test
public void test() {
assertEquals(fExpected, Fibonacci.fib(fInput));
}
}
你好,我真的是 JUnit 测试的新手,今天做了很多研究。通过自我、尝试和箭头取得了很多成就,但我一直遇到一个问题。我正在尝试为动态斐波那契计算编写一个随机测试。测试是 运行 在集合检查中手动添加的基本案例。他们在工作。我想添加更多案例,它们由 .txt
文件控制。输入也有效,但我无法稍后将对象添加到我的集合中。这是我的方法,但它在第 30 行抛出 java.lang.UnsupportedOperationExeption:
((List<Object[]>) check).add(i, new Object[][]{{0, new BigDecimal(Integer.parseInt(values[1]))}});
转换由 IntelliJ 完成。 尝试了很多不同的集合和列表可能性,但我被困在这里。
谢谢你帮我。
您用 Arrays.asList()
创建了 check
。 List
是基于输入数组的,所以不能扩展。
一种常见的方法是将该列表复制到 ArrayList
,这将使其可扩展。
是这样的:
Collection<Object[]> check = Arrays.asList(new Object[][]{{0, new BigDecimal(0)}, {1, new BigDecimal(1)},{2, new BigDecimal(1)}});
check = new ArrayList<Object[]>( check ); // copy to mutable ArrayList
为避免 ugly/uncessary 类型转换,您可以检查 List
类型。 List
扩展 Collection
所以它是兼容的。