The loop without a build tool
The loop of a test-driven project is four steps and it does not need a build tool: write a test, watch it fail, fix the code, watch it pass. This page runs the four steps on a class with a real bug, so the red report and the green report are the output of the same two commands.
The loop
The value of the loop is the second step. A test that has never failed has not been shown to test anything: it may assert nothing, call nothing or expect the wrong value, and a green run would look the same. A red run proves that the assertion can fail and that the report points at the code, and the fix then proves that the test was about the code and not about the phase of the moon.
A class with a bug
The class turns a title into a slug: lower case, spaces to dashes. It forgets to trim the ends, so a padded title keeps a dash at each end. The main shows the bug in one line, because the input it prints has spaces on both sides:
public class Slug {
static String make(String title) {
return title.toLowerCase().replace(' ', '-');
}
public static void main(String[] args) {
System.out.println(make(" Hello World "));
}
}
Two tests state the contract. The first is the ordinary case and passes; the second is the one the class gets wrong, and it is the test that was written first in a project that works this way:
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(MethodOrderer.MethodName.class)
public class SlugTest {
@Test
void turnsSpacesIntoDashes() {
assertEquals("hello-world", Slug.make("Hello World"));
}
@Test
void trimsTheEnds() {
assertEquals("hello", Slug.make(" Hello "));
}
}
javac -Xlint:all -cp /opt/java-tools/junit-platform-console-standalone.jar -d out Slug.java SlugTest.java
java -cp out Slug
-hello-world-
The dashes at the two ends of the output are the bug, visible without a test. The test exists because a person reading the program has to notice them, and a run does not.
The red run
The launcher runs the two tests and one of them fails. The tree names the method, the message and the two values, and the failure block under it has the stack trace down to the line of the test:
junit execute --class-path out --select-class SlugTest --disable-ansi-colors
echo "status: $?"
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│ └─ SlugTest ✔
│ ├─ trimsTheEnds() ✘ expected: <hello> but was: <--hello-->
│ └─ turnsSpacesIntoDashes() ✔
└─ JUnit Vintage ✔
Failures (1):
JUnit Jupiter:SlugTest:trimsTheEnds()
MethodSource [className = 'SlugTest', methodName = 'trimsTheEnds', methodParameterTypes = '']
=> org.opentest4j.AssertionFailedError: expected: <hello> but was: <--hello-->
org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1145)
SlugTest.trimsTheEnds(SlugTest.java:16)
java.base/java.lang.reflect.Method.invoke(Method.java:580)
java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Test run finished after 152 ms
[ 4 containers found ]
[ 0 containers skipped ]
[ 4 containers started ]
[ 0 containers aborted ]
[ 4 containers successful ]
[ 0 containers failed ]
[ 2 tests found ]
[ 0 tests skipped ]
[ 2 tests started ]
[ 0 tests aborted ]
[ 1 tests successful ]
[ 1 tests failed ]
status: 1
The fix
The failure names the value and the method, and the fix is one call: trim the title before the rest. The file below is the whole class with the change, which is what makes it compilable on its own; a reader diffing it against the first block sees one trim().
public class Slug {
static String make(String title) {
return title.trim().toLowerCase().replace(' ', '-');
}
public static void main(String[] args) {
System.out.println(make(" Hello World "));
}
}
The test did not change, which is the point of the order: the test was written from the contract, not from the implementation, so it stays the same while the implementation changes. A test that had to be edited to pass would have been a description of the bug.
The green run
javac -Xlint:all -cp /opt/java-tools/junit-platform-console-standalone.jar -d out Slug.java SlugTest.java
java -cp out Slug
hello-world
junit execute --class-path out --select-class SlugTest --disable-ansi-colors
echo "status: $?"
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│ └─ SlugTest ✔
│ ├─ trimsTheEnds() ✔
│ └─ turnsSpacesIntoDashes() ✔
└─ JUnit Vintage ✔
Test run finished after 570 ms
[ 4 containers found ]
[ 0 containers skipped ]
[ 4 containers started ]
[ 0 containers aborted ]
[ 4 containers successful ]
[ 0 containers failed ]
[ 2 tests found ]
[ 0 tests skipped ]
[ 2 tests started ]
[ 0 tests aborted ]
[ 2 tests successful ]
[ 0 tests failed ]
status: 0
JUnit 4 through the same jar
A project that still has JUnit 4 tests does not need a second runner. The standalone launcher carries the JUnit 4 API and the Vintage engine that runs it, so an org.junit.Test compiles against this one jar and appears under JUnit Vintage in the report:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class OldTest {
@Test
public void addsTwoNumbers() {
assertEquals(5, 2 + 3);
}
}
javac -Xlint:all -cp /opt/java-tools/junit-platform-console-standalone.jar -d out OldTest.java
junit execute --class-path out --select-class OldTest --disable-ansi-colors
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
└─ JUnit Vintage ✔
└─ OldTest ✔
└─ addsTwoNumbers ✔
Test run finished after 145 ms
[ 4 containers found ]
[ 0 containers skipped ]
[ 4 containers started ]
[ 0 containers aborted ]
[ 4 containers successful ]
[ 0 containers failed ]
[ 1 tests found ]
[ 0 tests skipped ]
[ 1 tests started ]
[ 0 tests aborted ]
[ 1 tests successful ]
[ 0 tests failed ]
The JUnit 4 class belongs to no Jupiter container, which is why the tree puts it under the Vintage engine and JUnit Jupiter has no child. The two kinds of test can run in one invocation, and a project can migrate one class at a time: the annotation changes, the runner does not.
What is not in the image
Three names come up in every Java testing conversation and none of them is in this image as a library of its own. The folder of tools holds exactly three jars, and the listing is the answer:
ls /opt/java-tools
checkstyle-all.jar
google-java-format-all-deps.jar
junit-platform-console-standalone.jar
Mockito makes a stand-in for a collaborator. A test that imports it here does not compile: the compiler answers package org.mockito does not exist, which is the whole story. A test can still be written without it, by writing a small class that implements the interface, which is the pattern the Java course uses.
AssertJ is a fluent assertion library: assertThat(list).hasSize(3). The assertions of JUnit cover the same ground in a plainer form, and the pages of this group use them. Maven Surefire is the plugin that makes Maven run tests: it is a download, and the Maven of lesson 128 cannot download anything in this container. The launcher jar is what runs the tests there, by hand, which is the subject of this group.
A project on a machine with a network adds all three to its build file and never sees the difference. The point of the pages here is that the framework itself needs one jar, so a reader can learn it without a build tool in the way.
What comes next
The group ends here, and the hub links its eight pages. The Java course covers the same tool from the lesson side in lesson 127, and the linters and the failed Maven build are in lesson 128.
The code on this page was compiled with OpenJDK 21.0.13 (javac) and run with the JUnit Platform Console Launcher 1.14.3 on Alpine Linux in a Docker container. The time in each report is that machine's and changes between runs.
Article author: Arthur Isaev