java – getRequests()必须返回一个Iterable数组
发布时间:2020-12-15 04:45:44 所属栏目:Java 来源:网络整理
导读:我的代码: @RunWith(Parameterized.class) public class FreshResultCompareRunner2 { //This is called before @BeforeClass ! @Parameterized.Parameters public static Collection getRequests() throws IOException { injector = Guice.createInjector(
我的代码:
@RunWith(Parameterized.class) public class FreshResultCompareRunner2 { //This is called before @BeforeClass ! @Parameterized.Parameters public static Collection getRequests() throws IOException { injector = Guice.createInjector(new MainModule()); initStaticFromInjector(); initTestInput(); return OrganizeTestParameterizedInput(); } private static void initTestInput() throws IOException { } private static Collection OrganizeTestParameterizedInput() { Object[] objectMatrix = new Object[100]; for (int i = 0; i < 100; i++) { objectMatrix[i] = i; } return Arrays.asList(objectMatrix); } 返回以下异常: getRequests()必须返回一个Iterable数组 我如何只增加int作为输入参数运行参数化junit? 比如i = 0 … 100运行相同的测试? 更新 我试过了 //This is called before @BeforeClass ! @Parameterized.Parameters public static Collection<int[]> getParameters() { injector = Guice.createInjector(new MainModule()); initStaticFromInjector(); int numOfChunks = 3;//routingResponseShortRepository.getNumOfBaseLineChunks(); //might be less int totalResponses = numOfChunks * globalSettings.requestsChunkSize; Collection<int[]> params = new ArrayList<>(totalResponses); for(int i = 1; i <= totalResponses; ++i) { params.add(new int[] { i }); } return params; } //takes the next matrix row from OrganizeTestParameterizedInput() public FreshResultCompareRunner2(int responseId) { this.responseId = responseId; } 并仍然得到一个错误: java.lang.Exception: com.waze.routing.automation.runners.FreshResultCompareRunner2.getParameters() must return an Iterable of arrays. at org.junit.runners.Parameterized.parametersMethodReturnedWrongType(Parameterized.java:343) 解决方法
对于参数化测试,JUnit将测试参数传递给测试类的构造函数.因为构造函数可以使用多个单个参数,所以JUnit期望每个参数集都是一个数组.数组的元素必须符合构造函数参数.
因此,您的配置方法必须返回一个Iterable数组,例如收集和LT;对象[]取代.在您的情况下,每次运行只有一个参数,因此您的数组的长度为1: @Parameterized.Parameters public static Collection<Object[]> getParameters() { Collection<Object[]> params = new ArrayList<>(100); for(int i = 1; i <= 100; ++i) { params.add(new Object[] { i }); } return params; } 另请注意,您的配置方法绝不应该像您的方法那样进行任何初始化!初始化只在@Before或@BeforeClass中完成! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |