domingo, 24 de noviembre de 2019

JUnit5 Jupiter (10) @ParameterizedTest @MethodSource

@MethodSource

public class MethodSourceDemoTest {

    @ParameterizedTest    @MethodSource( "dataStringProvider" )
    void testWithExplicitLocalMethodSource( String argument) {
        assertNotNull( argument);    }

    static Stream<String> dataStringProvider() {
        return Stream.of ("Estos","datos","generados","por método");    }

    @ParameterizedTest    @MethodSource    void dataStringProvider( String argument) {
        assertNotNull( argument);    }

    @ParameterizedTest    @MethodSource("range")
    void testWithRangeMethodSource( int argument) {
        assertNotEquals( 9, argument);    }

    static IntStream range() {
        return IntStream.range(0,20).skip(10);    }

    @ParameterizedTest    @MethodSource("stringIntAndListProvider")
    void testWithMultiArgMethodSource( String str, int num, List<String> list) {
        assertEquals( 5, str.length());        assertTrue( num >= 1 && num <= 2);        assertEquals( 2, list.size());    }

    static Stream<Arguments> stringIntAndListProvider() {
        return Stream.of(
            arguments("cinco", 1, Arrays.asList("a","b")),            arguments("abcde", 2, Arrays.asList("1","2"))
        );    }

    @ParameterizedTest    @MethodSource("com.external.providers.StringProviders#nombresStrings")
    void testWithExternalMethodSource( String tinyString) {
        assertNotNull( tinyString);    }
}

External Method

package com.external.providers;
import java.util.stream.Stream;
public class StringProviders {
    static Stream<String> nombresStrings() {
        return Stream.of("Jose","Alberto","Guille");    }
}

Run Test



eot

No hay comentarios:

Publicar un comentario