Junit 5 TestReporter

Junit 5 creates a descriptive test result report at the end of test execution. If required we can inject TestReporter dependency in @Test method and add a description in it.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AppleCalculatorTest {

    @Test
    void addApple(TestReporter reporter) {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
        reporter.publishEntry("1 apple + 1 apple is 2 apple");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

JUnit 5 TestReporter can be injected not only in a test case method but in all the JUnit 5 life cycle methods like @BeforeAll, @BeforeEach, @AfterEach and @AfterAll. TestReporter can also be injected into @ParameterizedTest as well.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }

    public boolean isApple(String appleName) {
        return "McIntosh".equalsIgnoreCase(appleName);
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class AppleCalculatorTest {
    @AfterAll
    static void afterAll(TestReporter reporter) {
        reporter.publishEntry(" AppleCalculatorTest.afterAll ");
    }

    @BeforeAll
    static void beforeAll(TestReporter reporter) {
        reporter.publishEntry(" AppleCalculatorTest.beforeAll ");
    }

    @BeforeEach
    void setUp(TestReporter reporter) {
        reporter.publishEntry(" AppleCalculatorTest.setUp ");
    }

    @AfterEach
    void tearDown(TestReporter reporter) {
        reporter.publishEntry(" AppleCalculatorTest.tearDown ");
    }

    @Test
    void addApple(TestReporter reporter) {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), " 1 apple + 1 apple is 2 apple ");
        reporter.publishEntry("1 apple + 1 apple is 2 apple ");
    }

    @ParameterizedTest
    @ValueSource(strings = {"McIntosh"})
    void isAppleTest(String appleName, TestReporter reporter) {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertTrue(appleCalculator.isApple(appleName), " yes its an apple ");
        reporter.publishEntry(" yes " + appleName + " an apple ");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

We can add as many description in TestReporter as required.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AppleCalculatorTest {
    @Test
    void addApple(TestReporter reporter) {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), " 1 apple + 1 apple is 2 apple ");
        reporter.publishEntry(" this test case is to assert Apple Calculator addition functionality ");
        reporter.publishEntry("1 apple + 1 apple is 2 apple");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

JUnit 5 TestReporter accepts map as well, so if required we can pass a map of descriptive text.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AppleCalculatorTest {
    @Test
    void addApple(TestReporter reporter) {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), " 1 apple + 1 apple is 2 apple ");
        Map<String, String> descriptionMap = new HashMap<>();
        descriptionMap.put("test case description", " this test case is to assert Apple Calculator addition functionality ");
        descriptionMap.put("test case assert description ", "1 apple + 1 apple is 2 apple");
        reporter.publishEntry(descriptionMap);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Some people use System.out.println to print the input or output of the test case, instead TestReporter.

package org.wesome.junit5;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package org.wesome.junit5;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AppleCalculatorTest {
    @Test
    void addApple(TestReporter reporter) {
        AppleCalculator appleCalculator = new AppleCalculator();
        int apple = appleCalculator.addApple(1, 1);
        assertEquals(2, apple, " 1 apple + 1 apple is 2 apple ");
        Map<String, String> descriptionMap = new HashMap<>();
        descriptionMap.put("1 apple + 1 apple is", apple + " apple");
        reporter.publishEntry(descriptionMap);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

follow us on