Testing Java code
A test is a method that calls the code and compares what came back with what should
have. JUnit is the framework that finds those methods, runs them and reports, and the
JDK does not contain it: this image carries the standalone launcher, one jar with the
framework and the program that runs it.
These pages use that jar and nothing else. Every test class here was compiled with
javac and run with junit in the
container that wrote the page, and every report is the one the run printed. The pages
are independent of the course: read one when its subject comes up, and follow the
links into the Java course for the full lesson.
Test
The launcher and the first test
The standalone launcher jar of the image, one test class, and the report junit execute prints.
Open
Assert
Assertions and a failure report
assertEquals, assertTrue and assertNull, the message an assertion prints, and a red run with its counts.
Open
Cases
Parameterised tests
One test method with many cases, through @ValueSource, @CsvSource and @MethodSource.
Open
Setup
Lifecycle: before and after
@BeforeAll, @BeforeEach, @AfterEach and @AfterAll, and the order a test method sees them in.
Open
Throw
Exceptions in tests
assertThrows and assertDoesNotThrow, a message that is checked, and the cause of an exception.
Open
Temp
Temporary folders
@TempDir, a file written and read inside a test, and the folder the framework deletes.
Open
Tree
The layout of a test project
src/main and src/test, two javac runs, and the class path the launcher needs.
Open
Loop
The loop without a build tool
Compile, run, read the report, fix: the cycle by hand, and the tools this image does not have.
Open