miércoles, 20 de noviembre de 2019

JUnit5 Jupiter (2) Maven sunfire plugin

JUnit5 Jupiter (2)


Running All Test

On IntelliJ


 

 Maven sunfire plugin


With the sunfire plugin, we can run the test from command line 
>mvn test

add the plugin to the pom.
<build>    
  <plugins>
        <!-- Need at least 2.22.0 to support JUnit 5 -->        
    <plugin>            
      <groupId>org.apache.maven.plugins</groupId>            
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M4</version>            
      <configuration>                
         <groups>assertions</groups>                
         <excludedGroups>timeout</excludedGroups>            
      </configuration>        
    </plugin>
    <plugin>            
       <groupId>org.apache.maven.plugins</groupId>            
       <artifactId>maven-compiler-plugin</artifactId>            
       <version>3.8.1</version>            
           <configuration>                
           <source>1.8</source>                
           <target>1.8</target>            
           </configuration>        
    </plugin>
  </plugins>
</build>
Previously, need to set in the path the route to maven so we can run mvn

 D:\proy\labJUnit5>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.bext:labJUnit5 >-------------------------
[INFO] Building labJUnit5 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ labJUnit5 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ labJUnit5 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ labJUnit5 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\proy\labJUnit5\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ labJUnit5 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ labJUnit5 ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running AssertionsTest
[ERROR] Tests run: 11, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 0.183 s <<< FAILURE! - in AssertionsTest
[ERROR] AssertionsTest.timeoutExceeded  Time elapsed: 0.108 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: execution exceeded timeout of 10 ms by 91 ms
        at AssertionsTest.timeoutExceeded(AssertionsTest.java:106)

[ERROR] AssertionsTest.timeoutExceededWithPreemtiveTermination  Time elapsed: 0.015 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: execution timed out after 10 ms
        at AssertionsTest.timeoutExceededWithPreemtiveTermination(AssertionsTest.java:113)

[INFO] Running AssumptionsTest
[WARNING] Tests run: 3, Failures: 0, Errors: 0, Skipped: 2, Time elapsed: 0.007 s - in AssumptionsTest
[INFO] Running DisplayNameGeneratorTest
[INFO] Running DisplayNameGeneratorTest$A_year_is_a_leap_year
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.045 s - in DisplayNameGeneratorTest$A_year_is_a_leap_year
[INFO] Running DisplayNameGeneratorTest$A_year_is_not_supported
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in DisplayNameGeneratorTest$A_year_is_not_supported
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 s - in DisplayNameGeneratorTest
[INFO] Running DisplayNamesTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 s - in DisplayNamesTest
[INFO] Running EnvironmentVariableConditionTest
[WARNING] Tests run: 2, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.003 s - in EnvironmentVariableConditionTest
[INFO] Running FirstJUnitJupiterTests
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in FirstJUnitJupiterTests
[INFO] Running JavaRunTimeEnvironmentConditionsTest
[WARNING] Tests run: 3, Failures: 0, Errors: 0, Skipped: 2, Time elapsed: 0.004 s - in JavaRunTimeEnvironmentConditionsTest
[INFO] Running OperativeSystemConditionsTest
[WARNING] Tests run: 5, Failures: 0, Errors: 0, Skipped: 4, Time elapsed: 0.005 s - in OperativeSystemConditionsTest
[INFO] Running ScriptBasedConditionsTest
Warning: Nashorn engine is planned to be removed from a future JDK release
Warning: Nashorn engine is planned to be removed from a future JDK release
[WARNING] Tests run: 5, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.656 s - in ScriptBasedConditionsTest
[INFO] Running StandardTest
[ERROR] Tests run: 4, Failures: 1, Errors: 0, Skipped: 2, Time elapsed: 0.007 s <<< FAILURE! - in StandardTest
[ERROR] StandardTest.failingTest  Time elapsed: 0.003 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: una prueba fallida
        at StandardTest.failingTest(StandardTest.java:23)

[INFO] Running SystemPropertyConditionsTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 s - in SystemPropertyConditionsTest
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR]   AssertionsTest.timeoutExceeded:106 execution exceeded timeout of 10 ms by 91 ms
[ERROR]   AssertionsTest.timeoutExceededWithPreemtiveTermination:113 execution timed out after 10 ms
[ERROR]   StandardTest.failingTest:23 una prueba fallida
[INFO]
[ERROR] Tests run: 46, Failures: 3, Errors: 0, Skipped: 12
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.986 s
[INFO] Finished at: 2019-11-21T14:25:33-06:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M4:test (default-test) on project labJUnit5: There are test failures.
[ERROR]
[ERROR] Please refer to D:\proy\labJUnit5\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

D:\proy\labJUnit5>

We can see al the test has been runned (46) 

09 Tagging and Filtering


First of all, Maven sunfire plugin needs to rename or test classes to match the format with can be identified for Testing, this is

• **/Test*.java 
• **/*Test.java 
• **/*Tests.java 
• **/*TestCase.java 

Tagging AssertionsTest class with respective Tag, and some inside methods with tag "timeout".
this is for filter the class AssertionTest and exclude the timeout method tags.

@Tag("assertions")
public class AssertionsTest {
    private final Calculator calculator = new Calculator(); 
    private final Persona persona = new Persona("Jose", "Martinez");
    @Test 
    void standardAssertions() { ... }

    @Test 
    void groupedAssertions() { ... }

    @Test 
    void dependentAssertions() {...}

    @Test     
    void exceptionTestingThrowException() {...}

    @Test     
    void exceptionTestingLambdaThrowException() {...}

    @Test 
    void exceptionTestingMethodThatThrowException() {...}
    
    @Test 
    @Tag("timeout")
    void timeoutNotExceeded(){  // será exitosa...}

    @Test 
    @Tag("timeout")
    void timeoutNotExceededWithResult() {...}

    @Test 
    @Tag("timeout")
    void timeoutNotExceededWithMethod(){...}

    @Test 
    @Tag("timeout")
    void timeoutExceeded() {...}

    @Test 
    @Tag("timeout")
    void timeoutExceededWithPreemtiveTermination() {...}

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


And Now we only want to run the test over this class exluding the timeout methods, for this modify the pom with
<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-surefire-plugin</artifactId>    <version>3.0.0-M4</version>    <configuration>        <groups>assertions</groups>        <excludedGroups>timeout</excludedGroups>    </configuration></plugin>

And run the test.

D:\proy\labJUnit5>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.bext:labJUnit5 >-------------------------
[INFO] Building labJUnit5 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ labJUnit5 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ labJUnit5 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ labJUnit5 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\proy\labJUnit5\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ labJUnit5 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ labJUnit5 ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running AssertionsTest
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 s - in AssertionsTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.063 s
[INFO] Finished at: 2019-11-21T14:34:29-06:00
[INFO] ------------------------------------------------------------------------

D:\proy\labJUnit5>

Now we can see that only 6 test were made.

The same with IntelliJ is reached creating a new run configuration



10 Test Execution Order


@Tag("assertions")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class AssertionsTest {
    final static Logger logger = LogManager.getLogger( AssertionsTest.class);    private final Calculator calculator = new Calculator();    private final Persona persona = new Persona("Jose", "Martinez");
    @Test    @Order(11)
    void standardAssertions(TestInfo testinfo) {
        logger.info( "11 " + testinfo.getDisplayName());        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    @Order(10)
    void groupedAssertions(TestInfo testinfo) {
        logger.info( "10 " + testinfo.getDisplayName());        //En assersiones agrupadas todas son ejecutadas, y las fallas son reportadas juntas        assertAll("persona",                () -> assertEquals("Jose", persona.getNombre()),                () -> assertEquals("Martinez", persona.getApellido())
        );    }

    @Test    @Order(9)
    void dependentAssertions(TestInfo testinfo) {
        logger.info( "9 " + testinfo.getDisplayName());        //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    @Order(8)
    void exceptionTestingThrowException(TestInfo testinfo) {
        logger.info( "8 " + testinfo.getDisplayName());        Exception exception = assertThrows( IllegalArgumentException.class, () -> {
            throw new IllegalArgumentException("argumento ilegal");        });        assertEquals("argumento ilegal", exception.getMessage());    }

    @Test    @Order(7)
    void exceptionTestingLambdaThrowException(TestInfo testinfo) {
        logger.info( "7 " + testinfo.getDisplayName());        Exception exception = assertThrows( ArithmeticException.class,                () -> { throw new ArithmeticException("/ by zero"); });        assertEquals("/ by zero", exception.getMessage());    }

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

    @Test    @Tag("timeout")
    @Order(4)
    void timeoutNotExceededWithResult(TestInfo testinfo) {
        logger.info( "4 " + testinfo.getDisplayName());        // será exitosa, resultado devuelto.        String actualResult = assertTimeout( ofMinutes(2),                () -> { return "X resultado";} );        assertEquals("X resultado", actualResult);    }

    @Test    @Tag("timeout")
    @Order(3)
    void timeoutNotExceededWithMethod(TestInfo testinfo){
        logger.info( "3 " + testinfo.getDisplayName());        // assert invoca un referencia de método y regresa un objeto.        String saludoActual = assertTimeout( ofMinutes(2), AssertionsTest::saludo);        assertEquals("Hola, Mundito!", saludoActual);    }

    @Test    @Tag("timeout")
    @Order(2)
    void timeoutExceeded(TestInfo testinfo) {
        logger.info( "2 " + testinfo.getDisplayName());        // assert fallará org.opentest4j.AssertionFailedError: execution exceeded timeout of 10 ms by 91 ms        assertTimeout( ofMillis(10), () -> Thread.sleep(100));    }

    @Test    @Tag("timeout")
    @Order(1)
    void timeoutExceededWithPreemtiveTermination(TestInfo testinfo) {
        logger.info( "1 " + testinfo.getDisplayName());        // assert fallará org.opentest4j.AssertionFailedError: execution timed out after 10 ms        assertTimeoutPreemptively(ofMillis(10), ()->Thread.sleep(100));    }

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

Executing this test and logging the trace on file we get, with out order and then ordered.

15:54:29.619| INFO  | AssertionsTest  | 10 groupedAssertions(TestInfo)
15:54:29.629| INFO  | AssertionsTest  | 6 exceptionTestingMethodThatThrowException(TestInfo)
15:54:29.631| INFO  | AssertionsTest  | 2 timeoutExceeded(TestInfo)
15:54:29.739| INFO  | AssertionsTest  | 9 dependentAssertions(TestInfo)
15:54:29.742| INFO  | AssertionsTest  | 8 exceptionTestingThrowException(TestInfo)
15:54:29.743| INFO  | AssertionsTest  | 5 timeoutNotExceeded(TestInfo)
15:54:29.745| INFO  | AssertionsTest  | 3 timeoutNotExceededWithMethod(TestInfo)
15:54:29.746| INFO  | AssertionsTest  | 11 standardAssertions(TestInfo)
15:54:29.747| INFO  | AssertionsTest  | 4 timeoutNotExceededWithResult(TestInfo)
15:54:29.749| INFO  | AssertionsTest  | 7 exceptionTestingLambdaThrowException(TestInfo)
15:54:29.750| INFO  | AssertionsTest  | 1 timeoutExceededWithPreemtiveTermination(TestInfo)


15:52:52.180| INFO  | AssertionsTest  | 1 timeoutExceededWithPreemtiveTermination(TestInfo)
15:52:52.207| INFO  | AssertionsTest  | 2 timeoutExceeded(TestInfo)
15:52:52.311| INFO  | AssertionsTest  | 3 timeoutNotExceededWithMethod(TestInfo)
15:52:52.313| INFO  | AssertionsTest  | 4 timeoutNotExceededWithResult(TestInfo)
15:52:52.314| INFO  | AssertionsTest  | 5 timeoutNotExceeded(TestInfo)
15:52:52.316| INFO  | AssertionsTest  | 6 exceptionTestingMethodThatThrowException(TestInfo)
15:52:52.317| INFO  | AssertionsTest  | 7 exceptionTestingLambdaThrowException(TestInfo)
15:52:52.318| INFO  | AssertionsTest  | 8 exceptionTestingThrowException(TestInfo)
15:52:52.320| INFO  | AssertionsTest  | 9 dependentAssertions(TestInfo)
15:52:52.324| INFO  | AssertionsTest  | 10 groupedAssertions(TestInfo)
15:52:52.326| INFO  | AssertionsTest  | 11 standardAssertions(TestInfo)

eot

No hay comentarios:

Publicar un comentario