java 在JUnit 5中参数化类和测试

vq8itlhq  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(165)

是否有一种方法可以同时参数化测试类(就像您可以对JUnit 4中的Parameterized@Parameters所做的那样)和测试方法(就像您可以对JUnit 4中的JUnitParams或JUnit 5中的@ParameterizedTest所做的那样)?最后,我需要获得参数的笛卡尔积。
使用所需方法对java.nio.ByteBuffer进行部分测试的示例:

public class ByteBufferTest {
    private static final int BUFFER_SIZE = 16384;
    private final ByteOrder byteOrder;
    private ByteBuffer sut;

    @Factory(dataProvider = "byteOrders")
    public ByteBufferTest(ByteOrder byteOrder) {
        this.byteOrder = byteOrder;
    }

    @DataProvider
    public static Object[][] byteOrders() {
        return new Object[][] {
                {ByteOrder.BIG_ENDIAN},
                {ByteOrder.LITTLE_ENDIAN}
        };
    }

    @BeforeMethod
    public void setUp() {
        sut = ByteBuffer.allocate(BUFFER_SIZE);
        sut.order(byteOrder);
    }

    @Test(dataProvider = "validPositions")
    public void position(int position) {
        System.out.println(byteOrder + " position " + position);
        sut.position(position);
        assertThat(sut.position()).isEqualTo(position);
    }

    @DataProvider
    public static Object[][] validPositions() {
        return new Object[][] {{0}, {1}, {BUFFER_SIZE - 1}};
    }

    @Test(dataProvider = "intPositionsAndValues")
    public void putInt(int position, int value, byte[] expected) {
        System.out.println(byteOrder + " position " + position + " value " + value);
        sut.putInt(position, value);
        assertThat(sut.array())
                .contains(expected[0], atIndex(position))
                .contains(expected[1], atIndex(position + 1))
                .contains(expected[2], atIndex(position + 2))
                .contains(expected[3], atIndex(position + 3));
    }

    @DataProvider
    public Object[][] intPositionsAndValues() {
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
            return new Object[][]{
                    {0, 0, new byte[4]},
                    {5, 123456789, new byte[] {0x07, 0x5B, (byte) 0xCD, 0x15}},
            };
        } else {
            return new Object[][]{
                    {0, 0, new byte[4]},
                    {5, 123456789, new byte[] {0x15, (byte) 0xCD, 0x5B, 0x07}},
            };
        }
    }
}

它产生:

LITTLE_ENDIAN position 0
LITTLE_ENDIAN position 1
LITTLE_ENDIAN position 16383
BIG_ENDIAN position 0
BIG_ENDIAN position 1
BIG_ENDIAN position 16383
LITTLE_ENDIAN position 0 value 0
LITTLE_ENDIAN position 5 value 123456789
BIG_ENDIAN position 0 value 0
BIG_ENDIAN position 5 value 123456789

我们正在考虑从TestNG迁移到JUnit 5,但是我们经常使用这种方法。在上面的例子中,字节顺序作为类级参数的使用并不是巧合:我们经常需要对各种二进制数据处理器进行测试,其中测试构造函数将接受字节/位顺序参数,并且我们对大端和小端运行每个测试。
我想为此创建一个扩展,然后使用ExtendWith,但也许有一个现有的扩展或其他东西,我错过了开箱即用?

ojsjcaue

ojsjcaue1#

JUnit木星(香草)

您可以在@MethodSource中组合多个源。基于TestNG示例:

class ExampleTest {

    @ParameterizedTest
    @MethodSource("args")
    void test(String classParameter, String testParameter) {
        System.out.println(classParameter + " " + testParameter);
    }

    static Stream<Arguments> args() {
        return classParameters().flatMap(
                classParameter -> testParameters().map(
                        testParameter -> Arguments.of(classParameter, testParameter)));
    }

    static Stream<String> classParameters() {
        return Stream.of("classParam1", "classParam2");
    }

    static Stream<String> testParameters() {
        return Stream.of("testParam1", "testParam2");
    }

}

这产生:

classParam1 testParam1
classParam1 testParam2
classParam2 testParam1
classParam2 testParam2

根据OP的要求,以下是"至少两种不同参数集的试验方法的示例":

class ExampleTest {

    static Stream<String> classParams() {
        return Stream.of("classParam1", "classParam2", "classParam3");
    }

    static Stream<Arguments> withClassParams(List<?> methodParams) {
        return classParams().flatMap(
                classParam -> methodParams.stream().map(
                        methodParam -> Arguments.of(classParam, methodParam)));
    }

    @ParameterizedTest
    @MethodSource
    void booleanParams(String classParam, boolean booleanParam) {
        System.out.println(classParam + " " + booleanParam);
    }

    static Stream<Arguments> booleanParams() {
        return withClassParams(List.of(false, true));
    }

    @ParameterizedTest
    @MethodSource
    void integerParams(String classParam, int integerParam) {
        System.out.println(classParam + " " + integerParam);
    }

    static Stream<Arguments> integerParams() {
        return withClassParams(List.of(1, 2, 3, 4, 5, 6));
    }

    @ParameterizedTest
    @MethodSource
    void objectParams(String classParam, Object objectParam) {
        System.out.println(classParam + " " + objectParam);
    }

    static Stream<Arguments> objectParams() {
        return withClassParams(List.of(new Object()));
    }

}

3个类参数加上3个具有不同类型和大小的不同方法参数,生成以下输出:

classParam1 java.lang.Object@35cabb2a
classParam2 java.lang.Object@35cabb2a
classParam3 java.lang.Object@35cabb2a
classParam1 1
classParam1 2
classParam1 3
classParam1 4
classParam1 5
classParam1 6
classParam2 1
classParam2 2
classParam2 3
classParam2 4
classParam2 5
classParam2 6
classParam3 1
classParam3 2
classParam3 3
classParam3 4
classParam3 5
classParam3 6
classParam1 false
classParam1 true
classParam2 false
classParam2 true
classParam3 false
classParam3 true

JUnit先锋

JUnit Jupiter有一个JUnit Pioneer扩展包,它附带了@CartesianTest。使用上面的扩展示例:

class CartProdTest {
    
    @CartesianTest
    @CartesianTest.MethodFactory("classWithObjectParams")
    void testClassWithObject(String clazz, Object object) {
        System.out.println(clazz + " " + object);
    }

    static ArgumentSets classWithObjectParams() {
        return ArgumentSets
                .argumentsForFirstParameter(classParams())
                .argumentsForNextParameter(new Object());
    }

    @CartesianTest
    @CartesianTest.MethodFactory("classWithIntegerParams")
    void testClassWithInteger(String clazz, int integerParam) {
        System.out.println(clazz + " " + integerParam);
    }

    static ArgumentSets classWithIntegerParams() {
        return ArgumentSets
                .argumentsForFirstParameter(classParams())
                .argumentsForNextParameter(1, 2, 3, 4, 5, 6);
    }

    @CartesianTest
    @CartesianTest.MethodFactory("classWithBooleanParams")
    void testClassWithBoolean(String clazz, boolean booleanParam) {
        System.out.println(clazz + " " + booleanParam);
    }

    static ArgumentSets classWithBooleanParams() {
        return ArgumentSets
                .argumentsForFirstParameter(classParams())
                .argumentsForNextParameter(false, true);
    }

    static String[] classParams() {
        return new String[]{"classParam1", "classParam2", "classParam3"};
    }

}

这将产生相同的输出。

相关问题