The layout of a test project

A single folder of sources works for a small example and fails for a project: the test classes would ship with the product. The convention that Maven and Gradle use puts the product in src/main/java and the tests in src/test/java, compiles them into two output folders, and runs the tests with both folders on the class path. This lesson does that by hand.

The two source folders

The layout has one package, app, in both trees. The product class compiles to out/main, the test class to out/test, and neither output folder contains the other's classes.

project/ ├── src/ │ ├── main/java/app/Greeter.java │ └── test/java/app/GreeterTest.java └── out/ ├── main/app/Greeter.class └── test/app/GreeterTest.class

The class under test

Greeter.java declares its package and one public method. A public class in a package still has to be in a file named after it, and the compiler writes it under the package folder in the output.

package app; public class Greeter { public static String greet(String name) { return "Hello, " + name + "!"; } }

The test class

GreeterTest.java is in the same package, so it calls Greeter without an import. A test in a different package would import app.Greeter, which only works when the class is public.

package app; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class GreeterTest { @Test void greetsByName() { assertEquals("Hello, Ada!", Greeter.greet("Ada")); } }

Two compilers, one run

The first compile builds the product with no JUnit at all: the product never imports it. The second compile builds the test, with the product folder and the launcher jar on the class path. The run names both folders so the launcher can load the test and the class it calls.

javac -Xlint:all -d out/main src/main/java/app/Greeter.java
javac -Xlint:all -cp out/main:/opt/java-tools/junit-platform-console-standalone.jar -d out/test src/test/java/app/GreeterTest.java
junit execute --class-path out/main:out/test --select-class app.GreeterTest --disable-ansi-colors

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│  └─ GreeterTest ✔
│     └─ greetsByName() ✔
└─ JUnit Vintage ✔

Test run finished after 439 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 report

The tree prints the simple class name, GreeterTest, not the package. The package is in the class file and in the --select-class argument; the report is for a person, and the simple name is enough when one run has one class of that name. Two classes called GreeterTest in different packages appear as two lines with the same text, which is a reason to name test classes after their subject.

Why the split

The product jar should not contain the tests, and a test may use a library the product does not ship. Two folders give the build a place to put each and a class path that sees the product when the test compiles. A build tool writes the same two commands and adds the dependency management; this course writes them by hand because the image has no build tool and the commands are the lesson.

The article on the project layout shows the same tree with the commands and the class path, for a reader who wants the short version.

Exercises

  1. Run the three commands in order and check the two output folders. Which folder holds Greeter.class?
  2. Put the test in the unnamed package instead and compile it against out/main. What must change in the source?
  3. Remove out/main from the run's class path and read the launcher error.
  4. Add a second test class and run with --scan-classpath instead of --select-class. How many tests does the report count?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which folder holds the product sources in the Maven layout?

    Answer

    src/main/java

    The product goes in src/main/java; the tests go in src/test/java.

  2. Why does the test compile need the product output folder on its class path?

    Answer

    The test calls the product class, which the compiler must resolve

    The test imports or calls the product, so javac needs the compiled product classes.

  3. What does the run class path need to contain?

    Answer

    The product output folders and the test output folder

    The launcher loads both the test and the class it calls from the class path.

  4. What does this program print? public class Q1 { public static void main(String[] args) { System.out.println("app.Greeter".substring(4)); } }

    Answer

    Greeter

    The substring starts after the first four characters, at Greeter.

  5. What does this program print? public class Q2 { public static void main(String[] args) { System.out.println("Hello, Ada!".length()); } }

    Answer

    11

    The greeting has eleven characters, spaces and punctuation included.

  6. What does this program print? public class Q3 { public static void main(String[] args) { String path = "out/main"; System.out.println(path.contains("/")); } }

    Answer

    true

    The path contains a slash between out and main.

  7. What does this program print? public class Q4 { public static void main(String[] args) { System.out.println("GreeterTest".endsWith("Test")); } }

    Answer

    true

    The name ends with Test, so the check is true.

  8. Why should the tests not be in the product jar?

    Answer

    They are not part of the product and may need libraries it does not ship

    The product ships only what a caller needs; tests are a development artifact.

  9. What does this program print? public class Q5 { public static void main(String[] args) { System.out.println("app".isEmpty()); } }

    Answer

    false

    The package name has three characters, so it is not empty.

  10. Which command compiles the test against the product?

    Answer

    javac -cp out/main:launcher.jar -d out/test Test.java

    The class path carries the product folder and the jar, and -d puts the test class in its own folder.

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