jueves, 21 de noviembre de 2019

JUnit5 Jupiter (4) @Nested, TestInfo, TestReporter, @ExtendWith

JUnit5 Jupiter (4)



11 Test Instance LifeCycle


@Tag("displayNames")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class DisplayNamesTest {
     final static Logger logger = LogManager.getLogger( DisplayNamesTest.class);
     StringBuffer forTestLifeCycle = new StringBuffer("varinitializedInClass");
 
    @Test 
    @DisplayName("Evaluation @DisplayName")
    void testWithDisplayNameContainingSpaces(){
        logger.info( "in methodA: " + forTestLifeCycle );
         forTestLifeCycle.append(" passed by methodA"); 
        logger.info( "in methodA: " + forTestLifeCycle );
         /* validation code */ 
    }
 
    @Test
    @DisplayName("© ¶§§µ¼¾½ΦλΔ ")
    void testWithDisplayNameContainingSpecialCharacters(){
        logger.info( "in methodB: " + forTestLifeCycle ); 
        forTestLifeCycle.append(" passed by methodB");
         logger.info( "in methodB: " + forTestLifeCycle ); 
       /* validation code */ 
    
 
    @Test 
    @DisplayName("\uD83D\uDE04")
    void testWithDisplayNameContainingEmoji(){
        logger.info( "in methodC: " + forTestLifeCycle );
         forTestLifeCycle.append(" passed by methodC"); 
         logger.info( "in methodC: " + forTestLifeCycle );
         /* validation code */ 
    }
}

The file Log, when instanced by class.

20:01:36.918| INFO  | DisplayNamesTest | in methodB: varinitializedInClass
20:01:36.920| INFO  | DisplayNamesTest | in methodB: varinitializedInClass passed by methodB
20:01:36.924| INFO  | DisplayNamesTest | in methodA: varinitializedInClass passed by methodB
20:01:36.924| INFO  | DisplayNamesTest | in methodA: varinitializedInClass passed by methodB passed by methodA
20:01:36.924| INFO  | DisplayNamesTest | in methodC: varinitializedInClass passed by methodB passed by methodA
20:01:36.925| INFO  | DisplayNamesTest | in methodC: varinitializedInClass passed by methodB passed by methodA passed by methodC

The file Log, when instanced per method. @TestInstance(TestInstance.Lifecycle.PER_METHOD)

20:08:11.615| INFO  | DisplayNamesTest | in methodB: varinitializedInClass
20:08:11.617| INFO  | DisplayNamesTest | in methodB: varinitializedInClass passed by methodB
20:08:11.621| INFO  | DisplayNamesTest | in methodA: varinitializedInClass
20:08:11.621| INFO  | DisplayNamesTest | in methodA: varinitializedInClass passed by methodA
20:08:11.623| INFO  | DisplayNamesTest | in methodC: varinitializedInClass
20:08:11.623| INFO  | DisplayNamesTest | in methodC: varinitializedInClass passed by methodC

12 Nested Test


@Tag("nestedStack")
@DisplayName("A Stack")
public class NestedStackTest {
    Stack<Object> stack;
    @Test    @DisplayName("Is instantiated with new Stack()")
    void isInstantiatedWithNew() {
        new Stack(); 
    }

    @Nested 
    @DisplayName("when new")
    class WhenNew {
        @BeforeEach 
        void createNewStack() {
            stack = new Stack();         
        }

        @Test 
        @DisplayName("is Empty")
        void isEmpty() {
            assertTrue(stack.empty()); 
        }

        @Test 
        @DisplayName(("throws EmptyStackException when popped"))
        void throwsExeptionWhenPopped() {
            assertThrows(EmptyStackException.class, stack::pop);         
        }

        @Test 
        @DisplayName(("throws EmptyStackException when peeked"))
        void throwsExeptionWhenPeeked() {
            assertThrows(EmptyStackException.class, stack::peek);         
        }

        @Nested 
        @DisplayName("after pushing an element")
        class AfterPushing {
            String anElement = "an Element";
            @BeforeEach 
            void pushAnElement() {
                stack.push( anElement); 
            }

            @Test 
            @DisplayName("not longer empty")
            void isNotEmpty() {
                assertFalse( stack.empty());             
            }

            @Test 
            @DisplayName("returns the element when popped and is Empty")
            void returnElementWhenPopped() {
                assertEquals( anElement, stack.pop()); 
                assertTrue(stack::empty); 
            }

            @Test 
            @DisplayName("returns the element when peeked but remains not empty")
            void returnElementWhenPeeked() {
                assertEquals( anElement, stack.peek());
                assertTrue(stack::empty);            }
        }
    }
}



13 Dependency Injection for Constructors ans Methods

 TestInfo
Using TestInfo as parameter to retrieve information about the current container or test.

@DisplayName("TestInfo test")
public class TestInfoTest {
    final static Logger logger = LogManager.getLogger( TestInfoTest.class);
    TestInfoTest(TestInfo testInfo) {
        assertEquals("TestInfo test", testInfo.getDisplayName());     
    }

    @BeforeEach 
    void init(TestInfo testInfo) {
        String displayName = testInfo.getDisplayName(); 
        logger.info("init: " + displayName);
        assertTrue( displayName.equals("TEST 1") || displayName.equals("test2()"));     
    }

    @Test 
    @DisplayName("TEST 1")
    @Tag("test1Tagged")
    void test1(TestInfo testInfo) {
        assertEquals( "TEST 1", testInfo.getDisplayName());         
        assertTrue( testInfo.getTags().contains("test1Tagged"));     
    }

    @Test 
    void test2(){
    }
}

Log file

21:44:03.545| INFO  | TestInfoTest    | init: TEST 1
21:44:03.552| INFO  | TestInfoTest    | init: test2()

Run Test


TestReporter

TestReporter to publish some additional data about the current test run. This way can be used as kind of logger.

public class TestReporterTest {
    StringBuffer sbtags = new StringBuffer();
    StringBuffer displayName = new StringBuffer();
    StringBuffer className = new StringBuffer(); 
    StringBuffer methodName = new StringBuffer();
 
    @BeforeEach     
    void init(TestInfo testInfo) {
        className.delete( 0, className.length()); 
        className.append( testInfo.getTestClass().get().getName());
        displayName.delete( 0, displayName.length());
        displayName.append( testInfo.getDisplayName()); 
        methodName.delete( 0, methodName.length()); 
        methodName.append( testInfo.getTestMethod().get().getName()); 
    }

    @Test 
    @DisplayName("testing on reportSingleValue")
    void reportSingleValue(TestReporter testReporter) {
        testReporter.publishEntry( "className  : " + className); 
        testReporter.publishEntry( "displayName: " + displayName); 
        testReporter.publishEntry("methodName  : " + methodName);        
        testReporter.publishEntry("algun mensaje de estatus"); 
    }

    @Test 
    void reportKeyValuePair(TestReporter testReporter) {
        testReporter.publishEntry( "className  : " + className); 
        testReporter.publishEntry( "displayName: " + displayName); 
        testReporter.publishEntry("methodName  : " + methodName); 
        testReporter.publishEntry("una Key", "un Value");     
    }

    @Test 
    void reportMultiKeyValuePairs(TestReporter testReporter) {
        Map<String, String> map = new HashMap<>(); 
        map.put("Fast and Furious 8","2018"); 
        map.put("Matrix","1999");
        testReporter.publishEntry( "className  : " + className); 
        testReporter.publishEntry( "displayName: " + displayName); 
        testReporter.publishEntry("methodName  : " + methodName); 
        testReporter.publishEntry(map); 
    }
}

Test Runned


@ExtendWith

@ExtendWith(com.example.random.RandomParametersExtension.class)
public class RandomParameterTest {

    @Test    void injectInteger(@RandomParametersExtension.Random int  i, @RandomParametersExtension.Random int  j) {
        assertNotEquals(i, j);    }

    @Test    void injectDouble(@RandomParametersExtension.Random double d) {
        assertEquals(0.0, d, 1.0);    }
}

Test Runned



eot

No hay comentarios:

Publicar un comentario