Reading the report
The launcher prints one report for a run, whether one test ran or a thousand. It has a tree at the top, a list of failures when there are any, and a table at the bottom. This lesson reads a green report with three tests, so the next lesson can show the same report with one failure added.
The program
Numbers.java has two methods: a sum over any number of arguments and a maximum. NumbersTest.java checks the empty sum, a sum of three values and a maximum, one test per method.
public class Numbers {
static int sum(int... values) {
int total = 0;
for (int value : values) {
total += value;
}
return total;
}
static int max(int... values) {
int best = values[0];
for (int value : values) {
if (value > best) {
best = value;
}
}
return best;
}
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class NumbersTest {
@Test
void sumOfNothingIsZero() {
assertEquals(0, Numbers.sum());
}
@Test
void sumOfSeveral() {
assertEquals(6, Numbers.sum(1, 2, 3));
}
@Test
void maxOfSeveral() {
assertEquals(9, Numbers.max(4, 9, 2));
}
}
javac -Xlint:all -cp /opt/java-tools/junit-platform-console-standalone.jar -d out Numbers.java NumbersTest.java
junit execute --class-path out --scan-classpath --disable-ansi-colors
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│ └─ NumbersTest ✔
│ ├─ sumOfSeveral() ✔
│ ├─ sumOfNothingIsZero() ✔
│ └─ maxOfSeveral() ✔
└─ JUnit Vintage ✔
Test run finished after 698 ms
[ 4 containers found ]
[ 0 containers skipped ]
[ 4 containers started ]
[ 0 containers aborted ]
[ 4 containers successful ]
[ 0 containers failed ]
[ 3 tests found ]
[ 0 tests skipped ]
[ 3 tests started ]
[ 0 tests aborted ]
[ 3 tests successful ]
[ 0 tests failed ]
The tree
The first line names the product and where to support it; the launcher prints it on every run. The tree below it has a root mark and four branches. JUnit Platform Suite is the suite the launcher built for this run, JUnit Jupiter is the engine that ran the methods, NumbersTest is the class, and the three leaves are the test methods. JUnit Vintage is the second engine, present even when no JUnit 4 test exists.
The check mark means the item succeeded. A failure replaces it with a cross and adds the first line of the assertion message on the same line, which is why a red tree can be read without the failure list below it. The order of the three methods is not the order of the source file: Jupiter sorts them by a rule of its own, and the course never relies on it.
The counts table
The table has two groups. The six containers lines count the things that hold tests: the platform, the engines, the suite and the class. The six tests lines count the methods. Found is what discovery saw, started is what began, successful and failed are the outcomes, and skipped and aborted are the two ways a test can end without a verdict.
Four containers are successful and none failed. Three tests were found, three started and three succeeded. A container is successful when every test inside it succeeded, so a class with one red test shows a red class line and a failed container count, while the other classes stay green.
The spaces inside the brackets line the columns up: the number is right-aligned in a field nine characters wide, and the word is followed by padding to the closing bracket. The launcher prints the spaces, and a page that quotes the table keeps them with , because HTML would collapse a run of ordinary spaces.
The time line
Test run finished after 698 ms is the one line that changes between two runs of the same tests. It measures the whole run, including discovery and the report, and it depends on the machine and on what else is running. The course checker normalises this number and compares every other line exactly, which is why a page can show a report from one run and still be checked on another.
Exit status and the shell
The launcher returns 0 when every test passed and 1 when at least one failed. The shell keeps that status in $?, so a script can stop on a red run without reading the report. Redirecting the report to a file and asking for the status prints just that one word:
junit execute --class-path out --scan-classpath --disable-ansi-colors > report.txt
echo "status: $?"
The report went to report.txt, and the shell printed status: 0: the status of the last command before echo. The next lesson makes one test fail and reads the status 1 that comes back.
Exercises
- Add a fourth test and predict the three counts that change before you run it.
- Run the same command twice and compare the time line. Which other line could change, and why does the checker allow it?
- Delete --scan-classpath and run. What does the launcher print, and what status does it return?
- Copy the counts table into a text editor and count the characters between the brackets. Which column is the number aligned to?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many containers does this course see for one test class?
Answer
4: the platform suite, Jupiter, the class and... the Vintage engine
The platform suite, the Jupiter engine, the test class and the Vintage engine are the four containers in the table.
-
Which line of a report changes between two runs of the same tests?
Answer
Test run finished after N ms
The run time belongs to the machine; every count and name stays the same.
-
What does a container count of 4 successful and 0 failed mean?
Answer
Every container that started finished without a failed test
Containers hold tests; a container is successful when all its tests are.
-
What does this program print?
public class Q1 { public static void main(String[] args) { int[] values = {4, 9, 2}; int best = values[0]; for (int value : values) { if (value != 4 && value != 2) { best = value; } } System.out.println(best); } }Answer
9
The loop keeps the value 9, which is neither 4 nor 2.
-
What does this program print?
public class Q2 { public static void main(String[] args) { int total = 0; for (int value : new int[] {1, 2, 3}) { total += value; } System.out.println(total); } }Answer
6
The loop adds the three elements of the array.
-
What does this program print?
public class Q3 { public static void main(String[] args) { int count = 0; for (int n = 0; n != 5; n++) { count++; } System.out.println(count); } }Answer
5
The loop runs for n = 0, 1, 2, 3 and 4, so count reaches 5.
-
What does this program print?
public class Q4 { public static void main(String[] args) { System.out.println("tests: " + (2 + 3)); } }Answer
tests: 5
The parentheses add the numbers before the string is joined to the result.
-
A run has found 3 tests, started 2, successful 1 and failed 1. What is the status of the launcher?
Answer
1, because at least one test failed
The launcher returns 1 when any test fails; a skipped or unstarted test does not change that.
-
Why does a page write the spaces of the counts table as
?Answer
HTML collapses runs of ordinary spaces, and the columns would lose their alignment
A browser prints one space for a run of spaces, so the alignment needs a non-breaking space.
-
What does this program print?
public class Q5 { public static void main(String[] args) { String[] names = {"Ada", "Grace"}; System.out.println(names.length); } }Answer
2
The array has two elements, and length is the number of elements.
The code on this page was built with OpenJDK 21.0.13 (javac) and run with JUnit Platform Console Launcher 1.14.3 on Alpine Linux in a Docker container.
Article author: Arthur Isaev