martes, 19 de noviembre de 2019

JUnit5 Jupiter (1)

JUnit5 Jupiter



01 Simple Test


02 StandardTest

public class StandardTest {
    @BeforeAll 
    static void initAll(){
    }

    @BeforeEach     
    void init(){
    }

    @Test    void succedingTest(){
        assertEquals(2,2);    }

    @Test    void failingTest(){
        fail("una prueba fallida");     
    }

    @Test 
    @Disabled("this test is disabled")
    void skippedTest(){
        // not executed 
    }

    @Test 
    void abortedTest(){
        assumeTrue("abc".contains("Z")); 
        fail("this should have been aborted"); 
    }

    @AfterEach     
    void tearDown(){}

    @AfterAll     
    static void tearDownAll(){}
}



03 @DisplayName

public class DisplayNamesDemo {
    @Test 
    @DisplayName("Evaluation @DisplayName")
    void testWithDisplayNameContainingSpaces(){
    }
    @Test 
    @DisplayName("© ¶§§µ¼¾½ΦλΔ ")
    void testWithDisplayNameContainingSpecialCharacters(){
    }
    @Test 
    @DisplayName("\uD83D\uDE04")
    void testWithDisplayNameContainingEmoji(){
    }
}


04 @DisplayNameGeneration


public class DisplayNameGeneratorDemo {
    @Nested 
    @DisplayNameGeneration(IndicativeSentences.ReplaceUnderscores.class)
    class A_year_is_not_supported {
        @Test 
        void if_it_is_zero(){ /* validation code here*/ }

        @DisplayName("Un valor negativo para el año no es soportado por el computo de año bisiesto.")
        @ParameterizedTest(name = "Ejemplo, año {0} no es soportado.")
        @ValueSource( ints = { -1, -4})
        void if_it_is_negative(int year){  /* validation code here*/ }
    }

    @Nested    @DisplayNameGeneration(IndicativeSentences.ReplaceUnderscores.class)
    class A_year_is_a_leap_year {
        @Test 
        void if_it_is_divisible_by_4_but_not_by_100(){ /* validation code here*/ }

        @DisplayName("....")
        @ParameterizedTest(name = "Año {0} es año bisiesto.")
        @ValueSource( ints = { 2016, 2020, 2048})
        void  if_it_is_one_of_the_following_years(int year){ /* validation code here*/ }
    }


    static class IndicativeSentences extends DisplayNameGenerator.ReplaceUnderscores{
        @Override 
        public String generateDisplayNameForClass(Class<?> testClass) {
            return super.generateDisplayNameForClass(testClass);        }

        @Override 
        public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
            return super.generateDisplayNameForNestedClass(nestedClass) + "...";        }

        @Override 
        public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
            String name = testClass.getSimpleName() + ' ' + testMethod.getName();            return name.replace('_',' ') + '.';
        }

    }
}




05Assertions


public class AssertionsDemo {
    private final Calculator calculator = new Calculator(); 
    private final Persona persona = new Persona("Jose", "Martinez");
    @Test 
    void standardAssertions() {
        assertEquals(2, calculator.add(1, 1)); 
        assertEquals(4, calculator.multiply(2, 2), "mensage opcional de falla es ahora el último parámetro."); 
        assertTrue('a' < 'b', () -> "Mensages assert pueden ser evaluados lazy para evitar construcciones complejas innecesarias"); 
    }

    @Test 
    void groupedAssertions() {
        //En assersiones agrupadas todas son ejecutadas, y las fallas son reportadas juntas 
        assertAll("persona", 
           () -> assertEquals("Jose", persona.getNombre()), 
           () -> assertEquals("Martinez", persona.getApellido())
        );     
    }

    @Test 
    void dependentAssertions() {
      //En un mismo bloque de codigo, si una assertion falla el código del mismo bloque será omitido. 
        assertAll("properties", 
           () -> {
                   String nombre = persona.getNombre(); 
                   assertNotNull( nombre);                    // se ejecuta solo si la assert anterior es valida. 
                   assertAll("nombre", 
                      () -> assertTrue( nombre.startsWith("J")), 
                      () -> assertTrue(nombre.endsWith("e"))
                   ); 
                 },
           () -> { // assert agrupados, se procesa independientemente del otro bloque, por lo tanto se ejecuta 
                   String apellido = persona.getApellido();
                     assertNotNull( apellido);                    // se ejecuta solo si la assert anterior es valida.
                     assertAll("apellido", 
                       () -> assertTrue(apellido.startsWith("M")),
                       () -> assertTrue( apellido.endsWith("z"))
                    );                }
         ); 
      }

    @Test    void exceptionTestingThrowException() {
        Exception exception = assertThrows( IllegalArgumentException.class, 
            () -> { throw new IllegalArgumentException("argumento ilegal"); 
                                          }); 
            assertEquals("argumento ilegal", exception.getMessage()); 
    }

    @Test    void exceptionTestingLambdaThrowException() {
        Exception exception = assertThrows( ArithmeticException.class,                () -> { throw new ArithmeticException("/ by zero"); });        assertEquals("/ by zero", exception.getMessage());    }

    @Test    void exceptionTestingMethodThatThrowException() {
        Exception exception = assertThrows( ArithmeticException.class,                () -> { calculator.divide(1,0); });        assertEquals("/ by zero", exception.getMessage());    }
    
    @Test    void timeoutNotExceeded(){  // será exitosa        assertTimeout(ofMinutes(2),                () -> { /* código ejecutado en menos de 2 minutos. */ } );    }

    @Test    void timeoutNotExceededWithResult() {
        // será exitosa, resultado devuelto.        String actualResult = assertTimeout( ofMinutes(2),                () -> { return "X resultado";} );        assertEquals("X resultado", actualResult);    }

    @Test    void timeoutNotExceededWithMethod(){
        // assert invoca un referencia de método y regresa un objeto.        String saludoActual = assertTimeout( ofMinutes(2), AssertionsDemo::saludo);        assertEquals("Hola, Mundito!", saludoActual);    }

    @Test    void timeoutExceeded() {
        // assert fallará org.opentest4j.AssertionFailedError: execution exceeded timeout of 10 ms by 91 ms        assertTimeout( ofMillis(10), () -> Thread.sleep(100));    }

    @Test    void timeoutExceededWithPreemtiveTermination() {
        // assert fallará org.opentest4j.AssertionFailedError: execution timed out after 10 ms        assertTimeoutPreemptively(ofMillis(10), ()->Thread.sleep(100));    }

    private static String saludo() {
        return "Hola, Mundito!";    }
}



06 Assumptions

public class AssumptionsDemo {

    private final Calculator calculator = new Calculator();
    @Test    void testOnlyOnContinuousIntegrationServer() {
        assumeTrue("CI".equals( System.getenv("ENV")) );    }

    @Test    void testOnlyOnDeveloperWorkstation() {
        assumeTrue("DEV".equals( System.getenv("ENV")), () -> "test abortada: no en estación de desarrollo.");    }

    @Test    void testInSpecificEnvironments() {
        // perform test if environment is Continuous Intergration        assumingThat("CI".equals( System.getenv( "ENV")),                () -> assertEquals( 2, calculator.divide(4,2)));        // perform test in all environment        assertEquals( 42, calculator.multiply( 6, 7));    }
}



07 @Disable

@Disabled("Tests para Clase Deshabilitada hasta pasar bug #12")
public class AssumptionsDemo {

    private final Calculator calculator = new Calculator();
    @Disabled("Test para método deshabilitada hasta pasar bug #15")
    @Test    void testOnlyOnContinuousIntegrationServer() {
        assumeTrue("CI".equals( System.getenv("ENV")) );    }

    @Test    void testOnlyOnDeveloperWorkstation() {
        assumeTrue("DEV".equals( System.getenv("ENV")), () -> "test abortada: no en estación de desarrollo.");    }

    @Test    void testInSpecificEnvironments() {
        // perform test if environment is Continuous Intergration        assumingThat("CI".equals( System.getenv( "ENV")),                () -> assertEquals( 2, calculator.divide(4,2)));        // perform test in all environment        assertEquals( 42, calculator.multiply( 6, 7));    }
}

07.1 Running al Test but @Disabled Test Class


07.2 @Disabled method test


08 Conditional Test Execution

08.1 Operative System Conditions

public class OperativeSystemConditions {
    @Test 
    @EnabledOnOs(WINDOWS)
    void onlyOnWindowsOs() {  /* validation code here*/    }

    @Test 
    @EnabledOnOs(MAC)
    void onlyOnMacOs() {  /* validation code here*/    }

    @TestOnMac    void testOnMac() {  /* validation code here*/    }

    @Test 
    @EnabledOnOs({LINUX, MAC})
    void onLinuxOrMac() { /* validation code here*/ }

    @Test 
    @DisabledOnOs(WINDOWS)
    void notOnWindows() {  /* validation code here*/ }

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Test 
    @EnabledOnOs(MAC)
    @interface TestOnMac {
        /* validation code here*/    }
}


08.2 Java Runtime Specific Conditions

public class JavaRunTimeEnvironmentConditions {
    @Test 
    @EnabledOnJre(JAVA_8)
    void onlyOnJava8() {
        /* validation code here*/    }

    @Test 
    @EnabledOnJre({JAVA_9, JAVA_10})
    void onJava9Or10() {
        /* validation code here*/    }

    @Test 
    @DisabledOnJre(JAVA_9)
    void notOnJava9() {
        /* validation code here*/    }
}


 08.3 System Property Conditions

public class SystemPropertyConditions {
    @Test 
    @EnabledIfSystemProperty(named = "os.arch", matches = ".*64.*")
    void onlyOn64BitArchitectures() { /* validation code here*/ }

    @Test 
    @DisabledIfSystemProperty(named = "ci-server", matches = "true")
    void notOnCiServer() { /* validation code here*/}
}


 08.4 Environment Variable Conditions

public class EnvironmentVariableCondition {
    @Test 
    @EnabledIfEnvironmentVariable(named = "ENV", matches = "staging-server")
    void onlyOnStagingServer() { /* validation code here*/ }

    @Test 
    @DisabledIfEnvironmentVariable(named = "ENV", matches = ".*development.*")
    void notOnDeveloperWorkstation() { /* validation code here*/ }

}


08.5 System Property Conditions


public class SystemPropertyConditions {
    @Test 
    @EnabledIfSystemProperty(named = "os.arch", matches = ".*64.*")
    void onlyOn64BitArchitectures() { /* validation code here*/ }

    @Test 
    @DisabledIfSystemProperty(named = "ci-server", matches = "true")
    void notOnCiServer() { /* validation code here*/}
}



08.6 Script Based Conditions

public class ScriptBasedConditions {
    @Test // Static JavaScript expression.    @EnabledIf("2 * 3 == 6")
    void willBeExecuted() {  }

    @RepeatedTest(10) // Dynamic JavaScript expression. 
    @DisabledIf("Math.random() < 0.314159") void mightNotBeExecuted() {  }
    @Test // Regular expression testing bound system property.     
    @DisabledIf("/32/.test(systemProperty.get('os.arch'))")
    void disabledOn32BitArchitectures() {
        assertFalse(System.getProperty("os.arch").contains("32"));    }

    @Test 
    @EnabledIf("'CI' == systemEnvironment.get('ENV')")
    void onlyOnCiServer() {
        assertTrue("CI".equals(System.getenv("ENV")));    }

    @Test // Multi-line script, custom engine name and custom reason. 
    @EnabledIf( value = {"load('nashorn:mozilla_compat.js')",                        "importPackage(java.time)",                        "",                        "var today = LocalDate.now()",                        "var tomorrow = today.plusDays(1)",                        "tomorrow.isAfter(today)"                        },            engine = "nashorn",            reason = "Self-fulfilling: {result}")
    void theDayAfterTomorrow() {
        LocalDate today = LocalDate.now();        LocalDate tomorrow = today.plusDays(1);        assertTrue(tomorrow.isAfter(today)); }
}



eot

No hay comentarios:

Publicar un comentario