The layout of a test project
Everything so far put the class and its test in one folder, and that is fine for a demonstration and wrong for a project. The convention every Java build tool follows is two source trees: the code under src/main/java and the tests under src/test/java, each in the package folders, and two output trees. The test code sees the code; the code never sees the tests, which is what keeps the test library out of the program.
The two source folders
The project of this page is small enough to see whole. The package demo appears in both trees, so the class and its test are in the same package and the test needs no import:
src/
main/java/demo/Calc.java
test/java/demo/CalcTest.java
out/
main/demo/Calc.class
test/demo/CalcTest.class
Nothing in the compiler knows those names. src/main/java is a convention that Maven and Gradle implement, and a project without a build tool keeps it by passing the right file names to javac. What the convention buys is that a reader knows where to look, and that the two compiles can have two class paths.
The class under test
The class is in the package demo and has a main, so it is a program and a subject at once. The package line is the first line of the file, and the file lives in a folder named after the package:
package demo;
public class Calc {
public static int add(int a, int b) {
return a + b;
}
public static int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
System.out.println("2 + 3 = " + add(2, 3));
System.out.println("4 * 5 = " + multiply(4, 5));
}
}
The test is in the same package, so it calls Calc.add without an import even though the two files are in different trees. A package is a name and not a folder: the compiler joins the two source trees into one namespace, which is exactly what a test of a package-private class needs.
The test class
Two tests, the class name ending in Test by the same convention the folders follow. The launcher does not need the name, because it finds the annotation, but a reader does:
package demo;
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 CalcTest {
@Test
void addsTwoNumbers() {
assertEquals(5, Calc.add(2, 3));
}
@Test
void multipliesTwoNumbers() {
assertEquals(20, Calc.multiply(4, 5));
}
}
Two compiles
The first command compiles the code alone, into out/main, with no launcher jar at all. The second compiles the test with three things on its class path: the launcher jar for the annotations, out/main for the class it tests, and the output folder of the test itself. The two runs are separate, so a failure in the test tree cannot break the program:
javac -Xlint:all -d out/main src/main/java/demo/Calc.java
java -cp out/main demo.Calc
2 + 3 = 5
4 * 5 = 20
javac -Xlint:all -cp /opt/java-tools/junit-platform-console-standalone.jar:out/main -d out/test src/test/java/demo/CalcTest.java
The second compile prints nothing, which is the sign that it worked: a warning or an error would be on the terminal. The output tree holds the package folder, so out/test/demo/CalcTest.class is what javac wrote, not out/test/CalcTest.class.
The run
The launcher needs both output folders on its class path, separated by the platform separator, which is a colon here. The class is named with its package:
junit execute --class-path out/main:out/test --select-class demo.CalcTest --disable-ansi-colors
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│ └─ CalcTest ✔
│ ├─ addsTwoNumbers() ✔
│ └─ multipliesTwoNumbers() ✔
└─ JUnit Vintage ✔
Test run finished after 195 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 ]
What the class paths do
The compiler uses its class path to resolve names; the launcher uses its class path to find classes. Both are lists of roots, and a package name is resolved by walking the folders under each root, which is why out/main and not out/main/demo is the entry. The same rule as lesson 006, one level up: the class path holds the root of the tree, and the package names do the rest.
A project with resources puts them in src/main/resources and copies them into the output tree, because a file read with getResourceAsStream is looked up on the same class path. A project with a test-only resource does the same in src/test/resources. That is the whole of the convention a build tool follows: two source roots, two resource roots, two output roots.
The separator between the entries is a colon on Linux and macOS and a semicolon on Windows, which is the one part of these commands that does not travel. A build tool exists to hide that and the rest of the list; this project is small enough that the list is the lesson.
What comes next
The last page of the group is the loop: compile, run, read the report, fix, and again. It also says what is not in this image, because a reader who expects Mockito or Maven Surefire should know before they look for them.
The Java course covers the package and the class path from lesson 006, the -d flag and the class file in lesson 003, and the layout a build tool expects 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 the report is that machine's and changes between runs.
Article author: Arthur Isaev