sábado, 23 de noviembre de 2019

JUnit5 Jupiter (6) Implementing many Interfaces with respective Test

JUnit5 Jupiter (6)  Implementing many Interfaces with respective Test


The TimeExecutionLogger Interface is extended by the class TimingExtencion with @ExtendsWith,


Observe that InterfaceLoggedTest and TimingExtension has a logger, by this way we generate two loggers with log4j2.

@Tag("timed")
@ExtendWith(TimingExtension.class)
public interface TimeExecutionLogger {
}

public interface InterfaceDynamicTest {
    @TestFactory    default Stream<DynamicTest> dynamicTestForPalindromes() {
        return Stream.of("asdfa","asdfas","adsf","asdf")
                .map( text -> dynamicTest( text, () -> assertTrue(isPalindrome(text))));    }

    default boolean isPalindrome(String text) {
        return true;    }; //just dummy}

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public interface InterfaceLoggedTest {
    static Logger logger = LogManager.getLogger(InterfaceLoggedTest.class.getName());
    @BeforeAll    default void beforeAllTest() {
        logger.info("Antes que todas las pruebas");    }

    @AfterAll    default void afterAllTest() {
        logger.info("Despues de todas las pruebas");    }

    @BeforeEach    default void beforeEachTest( TestInfo testInfo){
        logger.info( () -> String.format("antes   de %s", testInfo.getDisplayName()));    }

    @AfterEach    default void afterEachTest( TestInfo testInfo) {
        logger.info( ()-> String.format("despues de %s", testInfo.getDisplayName()));    }

    void thisMethodIsLoggedByInterface();    void alsoLoggedByInterface();
}


public class ImplemManyTimeInterfaceTest implements TimeExecutionLogger,
                               InterfaceLoggedTest, InterfaceDynamicTest {
    @Test    void isEqualValue() {
        assertEquals( 1, "a".length(), "siempre es Equals");    }

    @Override    public void thisMethodIsLoggedByInterface() {

    }

    @Override    public void alsoLoggedByInterface() {

    }
}

Log files generated with the test

20:55:58.077| INFO  | InterfaceLoggedTest | Antes que todas las pruebas
20:55:58.086| INFO  | InterfaceLoggedTest | antes   de dynamicTestForPalindromes()
20:55:58.102| INFO  | InterfaceLoggedTest | despues de dynamicTestForPalindromes()
20:55:58.104| INFO  | InterfaceLoggedTest | antes   de isEqualValue()
20:55:58.106| INFO  | InterfaceLoggedTest | despues de isEqualValue()
20:55:58.108| INFO  | InterfaceLoggedTest | Despues de todas las pruebas

20:55:58.101| INFO  | TimingExtension | metodo [dynamicTestForPalindromes] tiempo 13 ms.
20:55:58.106| INFO  | TimingExtension | metodo [isEqualValue] tiempo 2 ms.

   With JUnit5 Architecture can be achieved interesting and versatile test implementations.

eot

No hay comentarios:

Publicar un comentario