Junit 5 TestReporter
the dependency that can be injected in @Test
and Junit 5 life cycle methods to generate a descriptive test result report.
but descriptive is metadata and adding metadata about tests case should not be in test code because it is not a part of test, It is a meta-data. To achieve this, Junit Pioneer provides us @ReportEntry
annotation.
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.junitpioneer.jupiter.ReportEntry;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
@ReportEntry("1 apple + 1 apple is 2 apple ")
void addApple() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), " 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')
testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}
Just like Junit 5 TestReporter
we can pass as many descriptions as required in @ReportEntry
annotation.
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.junitpioneer.jupiter.ReportEntry;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
@ReportEntry(" this test case is to assert Apple Calculator addition functionality ")
@ReportEntry(" 1 apple + 1 apple is 2 apple ")
void addApple() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), " 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')
testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}
We can pass a map of descriptive text in @ReportEntry
the annotation as well.
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 org.junitpioneer.jupiter.ReportEntry;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
@ReportEntry(key = "test case description ", value = " this test case is to assert Apple Calculator addition functionality ")
@ReportEntry(key = "test case assert description ", value = "1 apple + 1 apple is 2 apple")
void addApple(TestReporter reporter) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), " 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')
testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}
Junit Pioneer @ReportEntry
provides all the features of Junit 5 TestReporter
, 1 additional feature that Junit Pioneer @ReportEntry
has is the conditional control over description in the test result report.
Junit Pioneer @ReportEntry
has enum PublishCondition which provides the condition for @ReportEntry
ALWAYS
it will always publish report after test run, irrespective of of test case, its the default value also.ON_SUCCESS
it will always publish report only if test case is successful.ON_FAILURE
it will always publish report only if test case failed.ON_ABORTED
it will always publish report only if test case aborted.
package com.example.junit5.sujan;
public class AppleCalculator {
public int addApple(int apple1, int apple2) {
return apple1 + apple2;
}
public String getApple() {
return "apple";
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ReportEntry;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junitpioneer.jupiter.ReportEntry.PublishCondition.ALWAYS;
import static org.junitpioneer.jupiter.ReportEntry.PublishCondition.ON_ABORTED;
import static org.junitpioneer.jupiter.ReportEntry.PublishCondition.ON_FAILURE;
import static org.junitpioneer.jupiter.ReportEntry.PublishCondition.ON_SUCCESS;
class AppleCalculatorTest {
@Test
@ReportEntry(key = "test case assert description ", value = "1 apple + 1 apple is 2 apple", when = ALWAYS)
void addAppleAlwaysTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), " 1 apple + 1 apple is 2 apple ");
}
@Test
@ReportEntry(key = "test case assert description ", value = "2 apple + 2 apple is 4 apple", when = ON_ABORTED)
void addAppleOnAbortedTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assumeFalse("apple".equalsIgnoreCase(appleCalculator.getApple()));
assertEquals(4, appleCalculator.addApple(2, 2), " 2 apple + 2 apple is 4 apple ");
}
@Test
@ReportEntry(key = "test case assert description ", value = "3 apple + 3 apple is 6 apple", when = ON_FAILURE)
void addAppleOnFailureTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(1, appleCalculator.addApple(3, 3), " 3 apple + 3 apple is 1 apple ");
}
@Test
@ReportEntry(key = "test case assert description ", value = "4 apple + 4 apple is 8 apple", when = ON_SUCCESS)
void addAppleOnSuccessTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(8, appleCalculator.addApple(4, 4), " 4 apple + 4 apple is 8 apple ");
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}