domingo, 24 de noviembre de 2019

JUnit5 Jupiter (9) @ParameterizedTest, @NullSource, @EmptySource, @NullAndEmptySource, @EnumSource




@NullSource, @EmptySource, @NullAndEmptySource

public class NullEmptyBlanksStringsTest {

    @ParameterizedTest 
    @NullSource     
    void nullString( String text) {
        assertTrue( text == null ); 
    }

    @ParameterizedTest 
    @EmptySource 
    @ValueSource( strings = {""," ","\t","\n"})
    void EmptyStrings(String text) {
        assertTrue( text.isEmpty() );     
    }

    @ParameterizedTest 
    @NullAndEmptySource 
    void nullAndEmptySourceNull( String text) {
        assertTrue( text == null ); 
    }

    @ParameterizedTest 
    @NullAndEmptySource 
    void nullAndEmptySourceEmpty( String text) {
        assertTrue( text.isEmpty() ); 
    }
}

 Run Test


@EnumSource

public class EnumSourceDemoTest {

    @ParameterizedTest 
    @EnumSource(TimeUnit.class)
    void testWithEnumSource( TimeUnit timeUnit){
        assertNotNull( timeUnit); 
    }

    @ParameterizedTest 
    @EnumSource( value = TimeUnit.class, names = { "DAYS", "HOURS" })
    void testWithEnumSourceInclude( TimeUnit timeUnit) {
        assertTrue( EnumSet.of( TimeUnit.DAYS, TimeUnit.HOURS).contains( timeUnit)); 
    }

    @ParameterizedTest 
    @EnumSource( value = TimeUnit.class, mode = EXCLUDE, names = {"DAYS","HOURS"})
    void testWithEnumSourceExclude( TimeUnit timeUnit) {
       assertFalse( EnumSet.of( TimeUnit.DAYS, TimeUnit.HOURS).contains( timeUnit)); 
       assertTrue( timeUnit.name().length() > 5); 
    }

    @ParameterizedTest 
    @EnumSource( value = TimeUnit.class, mode = MATCH_ALL, names = {"^(M|N).+SECONDS$"})
    void testWithEnumSourceRegEx( TimeUnit timeUnit) {
        String name = timeUnit.name(); 
        assertTrue( name.startsWith("M") || name.startsWith("N")); 
        assertTrue( name.endsWith("SECONDS")); 
    }
}

Run Test



eot



No hay comentarios:

Publicar un comentario