Changes

Jump to navigation Jump to search
2,310 bytes added ,  11:11, 22 June 2022
m
Line 138: Line 138:  
package com.luv2code.junitdemo;
 
package com.luv2code.junitdemo;
 
import org.junit.jupiter.api.Test;
 
import org.junit.jupiter.api.Test;
 +
// import org.junit.jupiter.api.DisplayName;
 +
import org.junit.jupiter.api.DisplayNameGeneration;
 +
import org.junit.jupiter.api.DisplayNameGenerator;
 
import static org.junit.jupiter.api.Assertions.*;
 
import static org.junit.jupiter.api.Assertions.*;
    +
// https://leeturner.me/posts/building-a-camel-case-junit5-displaynamegenerator/
 +
// @DisplayNameGeneration(DisplayNameGenerator.Simple.class)
 +
// @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
 +
@DisplayNameGeneration(DisplayNameGenerator.IndicativeSentences.class)
 
class DemoUtilsTest {
 
class DemoUtilsTest {
 +
    DemoUtils demoUtils;
 +
 +
    @BeforeAll
 +
    static void setUpClass() {
 +
        System.out.println("Cleanup before all tests")
 +
    }
 +
 +
    @BeforeEach
 +
    void setUp{
 +
        demoUtils = new DemoUtils();
 +
        int expected = 6;
 +
    }
 +
 
     @Test
 
     @Test
 +
    // @DisplayName("Null and Not Null")
 
     void testEqualsAndNotEquals() {
 
     void testEqualsAndNotEquals() {
        // Set up
  −
        DemoUtils demoUtils = new DemoUtils();
  −
        int expected = 6;
  −
   
       // Execute
 
       // Execute
 
       int actual = demoUtils.add(2, 4);
 
       int actual = demoUtils.add(2, 4);
    
       // Assert
 
       // Assert
       assertEquals(expected, actual, "2 + 4 must be 6")
+
       assertEquals(expected, actual, "2 + 4 must be 6");
 +
    }
 +
 
 +
    @Test
 +
    void testNullAndNotNull(){
 +
        String str1 = null;
 +
        String str2 = "luv2code";
 +
       
 +
        assertNull(demoUtils.checkNull(str1), "Object should be null");
 +
        assertNull(demoUtils.checkNull(str2), "Object should not be null");
 +
    }
 +
 
 +
    @AfterEach
 +
    void tearDownAfterEach(){
 +
        System.out.println("Cleanup After each test");
 +
    }
 +
 
 +
    @AfterAll
 +
    static void cleanUpClass(){
 +
        System.out.println("Cleanup After all tests");
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 +
==== Custom displayName generator for camelCase with numbers ====
 +
<syntaxhighlight lang="java">
 +
static class ReplaceCamelCaseEmojis extends ReplaceCamelCase {
 +
    public ReplaceCamelCaseEmojis() {
 +
    }
 +
 
 +
    public String generateDisplayNameForClass(Class<?> testClass) {
 +
        return this.replaceWithEmojis(super.generateDisplayNameForClass(testClass));
 +
    }
 +
 
 +
    public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
 +
        return this.replaceWithEmojis(super.generateDisplayNameForNestedClass(nestedClass));
 +
    }
 +
 
 +
    public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
 +
        return this.replaceWithEmojis(super.generateDisplayNameForMethod(testClass, testMethod));
 +
    }
 +
 
 +
    private String replaceWithEmojis(String name) {
 +
        name = name.replaceAll("Camel|camel", "\uD83D\uDC2B");
 +
        name = name.replaceAll("Case|case", "\uD83D\uDCBC");
 +
        name = name.replaceAll("Display|display", "\uD83D\uDCBB");
 +
        name = name.replaceAll("Divisible|divisible", "\u2797");
 +
        name = name.replaceAll("Year|year", "\uD83D\uDCC5");
 +
        name = name.replaceAll("100", "\uD83D\uDCAF");
 +
        return name;
 
     }
 
     }
 
}
 
}

Navigation menu