【问题标题】:Passing arrays to Parameterized JUnit将数组传递给参数化的 JUnit
【发布时间】:2014-06-11 17:15:05
【问题描述】:

我是 JUnit 4.x 的参数化功能的新手并且遇到了问题。我的参数化测试由 3 个整数数组组成,我很难声明它们。我下面的内容会产生运行时错误:

testGeneral[0] caused an ERROR: argument type mismatch
    argument type mismatch
    java.lang.IllegalArgumentException
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
testGeneral[1] caused an ERROR: argument type mismatch
    argument type mismatch
    java.lang.IllegalArgumentException
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

这是我的代码:

@RunWith(Parameterized.class)
public class MyArrayTest {
    private Integer[] inputList1;
    private Integer[] inputList2;
    private Integer[] expectedList;

    public MyArrayTest(Integer[] li1, Integer[] li2, Integer[] expected) {
        // ========> Runtime error happens here. <=========
        this.inputList1 = li1;
        this.inputList2 = li2;
        this.expectedList = expected;
    }

    @Parameterized.Parameters
    public static Collection testCases() {
        return Arrays.asList(new Object[][][] {
            {{1,1,1}, {2,2,2}, {3,3,3}},
            {{2,2,2}, {3,3,3}, {4,4,4}}
        });
    }

    @Test
    public void testGeneral() {
        // Do some test with this.inputList1, this.inputList2,
        // and verify with this.expectedList
        // I am not even getting here yet.
    }
}

感谢您帮助我正确地将三个数组传递给我的测试。

【问题讨论】:

    标签: java parameterized


    【解决方案1】:

    它失败的原因是因为您的测试需要整数数组,而您正在传递 Object 类型。所以你正在扩展类型。试试这个:

    @Parameterized.Parameters
    public static Collection testCases() {
        return Arrays.asList(new Integer[][][] {
            {{1,1,1}, {2,2,2}, {3,3,3}},
            {{2,2,2}, {3,3,3}, {4,4,4}}
        });
    }
    

    【讨论】:

    • 我觉得自己很笨。谢谢。
    • 如何用 kotlin 做到这一点?
    【解决方案2】:

    此解决方案使用junitparams,实现junitparams.converters.Converter 并将长值列表解析为参数。

    package example;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import java.util.Arrays;
    import java.util.stream.Collectors;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    
    import junitparams.JUnitParamsRunner;
    import junitparams.Parameters;
    import junitparams.converters.ConversionFailedException;
    import junitparams.converters.Converter;
    import junitparams.converters.Param;
    
    @RunWith(JUnitParamsRunner.class)
    public class LongArrayParameterTest {
    
        @Parameters({ "0|10", "1|10;20;30" })
        @Test
        public void test(final long otherParameter, @LongArrayParam final long[] expected) {
            System.out.println(Arrays.stream(expected).boxed().map(l -> Long.toString(l)).collect(Collectors.toList()));
        }
    
        @Retention(RetentionPolicy.RUNTIME)
        @Target(ElementType.PARAMETER)
        @Param(converter = LongArrayConverter.class)
        public @interface LongArrayParam {
    
        }
    
        public static class LongArrayConverter implements Converter<LongArrayParam, long[]> {
    
            @Override
            public void initialize(final LongArrayParam annotation) {
            }
    
            @Override
            public long[] convert(final Object param) throws ConversionFailedException {
                final String str = (String) param;
                final String[] longStrings = str.split(";");
                return Arrays.stream(longStrings).mapToLong(s -> Long.parseLong(s)).toArray();
            }
    
        }
    }
    

    此解析器不支持空列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多