参数化 Junit class - 如何将 ArrayList 作为参数发送到方法 ()
Parameterized Junit class - How to send an ArrayList as a parameter to a method()
考虑以下代码:
import static org.junit.Assert.assertEquals;
import java.util.Collection;
import java.util.List;
import org.assertj.core.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class StackFrustratedCoderTest {
private List<Integer> input;
private Integer expected;
private ItcStackFrustrated itcStack;
public StackFrustratedCoderTest(List<Integer> array, Integer expected){
this.input = array;
this.expected = expected;
}
@Before
public void init(){
itcStack = new ItcStackFrustrated();
}
@Parameterized.Parameters
public Collection parameterInput(){
return Arrays.asList(new Object[][] {{1,7,2,2,4,4}, 11}});
}
@Test
public void testFrustatedCoder(){
assertEquals(this.expected, itcStack.check(this.input));
}
}
考虑方法 itcStack.check() 是一个要测试的函数,作为参数,它需要 ArrayList 变量。
如何用下面的方法编码:
@Parameterized.Parameters
public Collection parameterInput(){
return Arrays.asList(new Object[][] {{1,7,2,2,4,4}, 11}});
}
以上代码显示编译错误。 {1,7,2,2,4,4} 是一个 int 数组,但我需要 ArrayList。任何建议表示赞赏。
此外,如果可以提供任何文章,其中解释了参数化 class 如何在内部运行。
这里是:
{1,7,2,2,4,4}
是一个文字,可以创建一个 int 数组。
直接去:
Arrays.asList(1, 7, 2, ...);
相反。
编辑
我们可以这样做。
int[] array = new int[]{1,7,2,2,4,4};
return Arrays.asList(new Object[][] {{Arrays.asList(array), 11}});
考虑以下代码:
import static org.junit.Assert.assertEquals;
import java.util.Collection;
import java.util.List;
import org.assertj.core.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class StackFrustratedCoderTest {
private List<Integer> input;
private Integer expected;
private ItcStackFrustrated itcStack;
public StackFrustratedCoderTest(List<Integer> array, Integer expected){
this.input = array;
this.expected = expected;
}
@Before
public void init(){
itcStack = new ItcStackFrustrated();
}
@Parameterized.Parameters
public Collection parameterInput(){
return Arrays.asList(new Object[][] {{1,7,2,2,4,4}, 11}});
}
@Test
public void testFrustatedCoder(){
assertEquals(this.expected, itcStack.check(this.input));
}
}
考虑方法 itcStack.check() 是一个要测试的函数,作为参数,它需要 ArrayList 变量。
如何用下面的方法编码:
@Parameterized.Parameters
public Collection parameterInput(){
return Arrays.asList(new Object[][] {{1,7,2,2,4,4}, 11}});
}
以上代码显示编译错误。 {1,7,2,2,4,4} 是一个 int 数组,但我需要 ArrayList。任何建议表示赞赏。
此外,如果可以提供任何文章,其中解释了参数化 class 如何在内部运行。
这里是:
{1,7,2,2,4,4}
是一个文字,可以创建一个 int 数组。
直接去:
Arrays.asList(1, 7, 2, ...);
相反。
编辑
我们可以这样做。
int[] array = new int[]{1,7,2,2,4,4};
return Arrays.asList(new Object[][] {{Arrays.asList(array), 11}});