Asserting Statistics
Asserting statistics is yet another important feature of the JUnit Platform Test Kit. Asserting statistics is used to assert events against the events that are generated during the execution of the test plan. The following code demonstrates the ways to assert statistics for tests and containers in a JUnit test engine.
import org.junit.jupiter.api.Test;
import org.junit.platform.testkit.engine.EngineTestKit;
class AssertExample {
@Test
void verifyContainerStats() {
EngineTestKit
.engine("junit-jupiter")
.selectors(selectClass(TestCase.class))
.execute()
.containerEvents()
.assertStatistics(stats -> stats.started(2).succeeded(2));
}
@Test
void verifyJupiterTestStats() {
EngineTestKit
.engine("junit-jupiter")
.selectors(selectClass(ExampleTestCase.class))
.execute()
.testEvents()
.assertStatistics(stats ->
stats.skipped(1).started(3).succeeded(1).aborted(1).failed(1));
}
}

You can also try this code with Online Java Compiler
Run Code
The following are the steps involved in Asserting Statistics,
- Select the JUnit Test Engine.
- Select the Test Case class.
- Execute the test plan.
- Filter by container events.
- Assert stats for container events.
- Filter by test events.
- Assert Stats for test events.
Asserting Events
If the Asserting Statistics are alone not sufficient for verifying the behavior of test execution, we opt for Asserting Events. We can work directly with the recorded event elements and perform assertions against them.
Let us look at an example code to understand the principles of asserting events,
import org.junit.jupiter.api.Test;
import org.junit.platform.testkit.engine.EngineTestKit;
import org.junit.platform.testkit.engine.Events;
class ExampleAssertingEvent {
@Test
void verifyMethodWasSkipped() {
String methodName = "testSkipped";
Events testEvents = EngineTestKit
.engine("junit-jupiter")
.selectors(selectMethod(TestCase.class, methodName))
.execute()
.testEvents();
testEvents.assertStatistics(stats -> stats.skipped(1));
testEvents.assertThatEvents()
.haveExactly(1, event(test(methodName),
skippedWithReason("demonstration")));
}
}

You can also try this code with Online Java Compiler
Run Code
The following are the steps involved in Asserting Events,
- Selecting the JUnit Jupiter Test Engine.
- Select the skipped test method in the Test Case.
- Execute the test plan.
- Filter by test events.
- Save the test events to a local variable.
- Optionally assert the expected stats.
- Assert that the recorded test events contain exactly one skipped test named testSkipped with “demonstration” as the reason.
FAQs
-
What is JUnit?
JUnit is a framework in Java used for unit testing. It is vital in the development of test-driven development. It is part of the xUnit frameworks. We link the JUnit as a JAR file at compile time. JUnit5 resides under the org.junit.jupiter package in Java.
-
What is the latest version of JUnit?
The most recent version of JUnit is Junit5, although the previous versions of Junit are still equally famous. For instance, JUnit4 has over 100,000 usages.
-
What is JUnit Platform Test Kit?
JUnit Platform Test Kit is yet another package in JUnit that is widely used for executing test plans on the JUnit Platform. We can also verify the obtained results using this package. The three primary features of Platform Test Kit in JUnit are,
Engine Test Kit
Asserting Statistics
Asserting Events
-
What is the significance of the Engine Test Kit?
The Engine Test Kit package supports executing a test plan for a given test engine running on the JUnit Platform and then gaining access to the results via a fluent API to verify the expected output.
-
What is the significance of Asserting Statistics?
Asserting statistics is another important feature of the JUnit Platform Test Kit. Asserting statistics is used to assert events against the events that are generated during the execution of the test plan.
Key Takeaways
This Blog covered all the necessary points about the JUnit Platform Test Kit and all the features of the JUnit Platform Test Kit. We further discussed how to implement the JUnit Platform Test Kit in Java.
Check out JUnit Interview Questions here.
Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.