Testing a Java program
A program's tests pass on a JDK, and the program rontolisp compiles is not the JDK's: the
subset lowers the language itself and has its own classes, so a suite that is
green under surefire says nothing certain of the compiled program. rontolisp test runs
the same test sources on the program rontolisp makes:
$ cat src/test/java/shop/CartTest.java
package shop;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class CartTest {
@Test
void anEmptyCartCostsNothing() {
assertThat(new Cart().total()).isZero();
}
@ParameterizedTest
@CsvSource({ "tea, 2, 900", "cup, 1, 1200" })
void aLineCostsItsPriceTimesItsCount(String item, int count, int total) {
assertThat(new Cart().add(item, count).total()).isEqualTo(total);
}
@Test
void anUnknownItemIsRefused() {
assertThatThrownBy(() -> new Cart().add("spoon", 1)).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("spoon");
}
}
$ rontolisp test --java-source-path src/main/java \
--java-source-dep org.junit.jupiter:junit-jupiter-params:6.0.3 src/test/java/shop/CartTest.java
Running shop.CartTest
Tests run: 4, Failures: 1, Errors: 0, Skipped: 0 <<< FAILURE! -- in shop.CartTest
shop.CartTest.aLineCostsItsPriceTimesItsCount[2] <<< FAILURE!
expected: 1200
but was: 1000
Results:
Tests run: 4, Failures: 1, Errors: 0, Skipped: 0
$ echo $?
1
The target is a test file, or several files of one source tree, run as one program, the
classes of the first file first, then the next's. The tests are found while the files are
compiled, as a compiled program has no class file to scan: every class of a file with a
test, and the @Nested classes in them. A further file is of the first file's tree, at the
path its package and class name spell:
rontolisp test --java-source-path src/main/java \
--java-source-dep org.junit.jupiter:junit-jupiter-params:6.0.3 \
src/test/java/shop/CartTest.java src/test/java/shop/stock/StockTest.java
A directory stands for the .java files below it, in the order of their paths, so
rontolisp test ... src/test/java runs every test of the tree. Which of its classes have
tests is found as for a file: a class with no test annotation of its own may inherit tests.
The code under test is read from the
source path (--java-source-path,
--java-source-dep) and compiled with the tests, so a test calls the package-private
members of its package as it does under javac.
JUnit Jupiter is a dependency of the program, as it is of the project, and is compiled from
its sources like any library: --java-source-dep org.junit.jupiter:junit-jupiter-params:VERSION, the version the project tests with,
brings its parameterized tests, its API and what they depend on (junit-jupiter-api is
enough for a file with no parameterized test). Its licence (EPL-2.0) is the program's
then, as for a run on a JDK (Licenses). A run without it says
so:
$ rontolisp test --java-source-path src/main/java src/test/java/shop/CartTest.java
error: src/test/java/shop/CartTest.java:12:5: the class org.junit.jupiter.api.Test is not on the source path: JUnit Jupiter is a dependency of the program, compiled from its sources (--java-source-dep org.junit.jupiter:junit-jupiter-params:VERSION)
AssertJ is not on the source path: the subset has a part of it of its own, under its names.
The status is 0 when every test passed, 1 when one failed, when what is compiled
is refused or when no test ran, and 2 for a wrong command line. A test that throws an
AssertionError is a failure and any other exception an error, as surefire counts them;
a run of a parameterized test is a test, named by its place among the runs. A test whose
assumption does not hold (assumeTrue, abort) is skipped; one in a @BeforeAll skips
every test of the class, each test and each @Nested class counted one, as surefire
counts them.
On the four backends
With no -o the tests run on the interpreter. With -o the run is compiled, and the
artifact has the same report and the same status:
T="rontolisp test --java-source-path src/main/java
--java-source-dep org.junit.jupiter:junit-jupiter-params:6.0.3
src/test/java/shop/CartTest.java"
$T # interpreter
$T -o CartTests.class && java CartTests # JVM
$T -o cart-tests.wasm && wasmtime run cart-tests.wasm # WASM preview 1
$T -o cart-tests-comp.wasm --component && \
wasmtime run cart-tests-comp.wasm # WASM component
A @TempDir is made in /tmp, which a .wasm reaches only where it is preopened:
wasmtime run --dir /tmp cart-tests.wasm.
A Maven project
The test goal of the Maven plugin
runs a project's tests this way at test, after surefire, on the backends <backends>
names, and fails the build with the name of a failed test.
JUnit Jupiter
| Annotation | What runs |
|---|---|
@Test | A method of no parameter but a @TempDir |
@ParameterizedTest | The method once for each row of its sources, then each argument that is AutoCloseable closed, the last first (not with autoCloseArguments = false): @ValueSource (every element but classes), @CsvSource (value, textBlock, delimiter, delimiterString, quoteCharacter, emptyValue, nullValues, ignoreLeadingAndTrailingWhitespace), @MethodSource (static methods of the class named by string literals, or the one named as the test: a Stream, an Iterable, an array, an IntStream, a LongStream or a DoubleStream, of Arguments, of arrays or of single arguments), @EnumSource (value, names, mode), @NullSource, @EmptySource, @NullAndEmptySource. A text is converted to a parameter of a primitive type, of its box or of an enum, and a number widens |
@BeforeEach, @AfterEach | Around each test, on its instance: those of a class above first before a test and last after it, those of the class around a @Nested class outside its own |
@BeforeAll, @AfterAll | Static methods, around the tests of the class and of its @Nested classes. One that throws is an error of the class, whose tests then do not run |
@Nested | The tests of an inner class, each on an instance made in an instance of the class around it |
@Disabled | A test counted as skipped, or a class: each of its tests and @Nested classes |
@TempDir | A field or a parameter of type Path or File: a directory of its own in /tmp for each, a static field's around the tests of its class, the others' around each test, deleted with what it holds after it |
@DisplayName | Of a test: written after the test's name where it fails |
A source written more than once gives its rows as JUnit gives them: the sources of one kind
together, where the first of them is written. A Named argument is its payload.
The assertions, the assumptions and the rest of JUnit Jupiter's API are JUnit's own,
compiled from its sources, so a failed assertion says what that version of JUnit says. Of
JUnit Jupiter 6.0.3, the subset refuses assertTimeoutPreemptively where a test calls it
(it reads a thread's stack).
A class inherits the tests of the classes of the program above it; a method overridden with no annotation is no test. The tests of a class run in the order declared, where JUnit runs them in an order of its own.
What there is of AssertJ
org.assertj.core.api.Assertions has assertThat of the kinds below, assertThatThrownBy,
assertThatCode, assertThatExceptionOfType (with assertThatIllegalArgumentException,
assertThatIllegalStateException, assertThatNullPointerException,
assertThatIOException), assertThatNoException, catchThrowable, fail and entry.
assertThat of | Checks |
|---|---|
| any value | isEqualTo (two arrays by their elements), isNotEqualTo, isNull, isNotNull, isSameAs, isNotSameAs, isInstanceOf, isNotInstanceOf, isExactlyInstanceOf, isInstanceOfSatisfying, isIn, isNotIn, hasToString, doesNotHaveToString, matches, satisfies, and as, describedAs, withFailMessage, overridingErrorMessage |
| an object | extracting with a function |
a String | isEmpty, isNotEmpty, isNullOrEmpty, isBlank, isNotBlank, hasSize and its four comparisons, contains, doesNotContain, containsIgnoringCase, containsSubsequence, startsWith, doesNotStartWith, endsWith, doesNotEndWith, isEqualToIgnoringCase, matches, doesNotMatch |
an int, long, float, double or its box | isZero, isNotZero, isOne, isPositive, isNegative, isNotNegative, isNotPositive, isGreaterThan, isGreaterThanOrEqualTo, isLessThan, isLessThanOrEqualTo, isBetween, isStrictlyBetween, isEqualByComparingTo; of a double isNaN, isNotNaN |
a char, a boolean | isGreaterThan, isLessThan; isTrue, isFalse |
an Iterable, a List | isEmpty, isNotEmpty, isNullOrEmpty, hasSize and its four comparisons, contains, containsAll, doesNotContain, containsExactly, containsExactlyElementsOf, containsExactlyInAnyOrder, containsExactlyInAnyOrderElementsOf, containsOnly, containsSequence, containsSubsequence, anyMatch, allMatch, noneMatch, allSatisfy, anySatisfy, extracting, map, filteredOn with a predicate, first, last, element, singleElement |
an array of objects, of byte, int, long, char or double | isEmpty, isNotEmpty, isNullOrEmpty, hasSize, contains, doesNotContain, containsExactly, containsExactlyInAnyOrder; of objects containsOnly, of a primitive type startsWith |
a Map | isEmpty, isNotEmpty, isNullOrEmpty, hasSize, containsKey, containsKeys, doesNotContainKey, doesNotContainKeys, containsValue, doesNotContainValue, containsEntry, doesNotContainEntry, contains, containsAllEntriesOf, containsOnly, containsOnlyKeys |
an Optional | isPresent, isNotEmpty, isEmpty, isNotPresent, contains, hasValue, hasValueSatisfying, get |
a Throwable | hasMessage, hasMessageContaining, hasMessageNotContaining, hasMessageStartingWith, hasMessageEndingWith, hasMessageMatching, hasNoCause, hasCauseInstanceOf, hasRootCauseInstanceOf, hasRootCauseMessage, cause, rootCause, doesNotThrowAnyException, and after isThrownBy the same under withMessage .. withRootCauseInstanceOf, havingCause, havingRootCause |
A failed check says what AssertJ says, and throws the class AssertJ throws beside JUnit
(AssertionFailedError where two values are compared, AssertionError otherwise). Where
AssertJ adds the frames of a stack trace or the name of a JDK's own class, the message has
none.
What is not there
An annotation of JUnit's that a run does not run, an element of one that it does not read, and a class or a check of AssertJ that the subset lacks are refused by name when the file is compiled, never skipped:
$ rontolisp test --java-source-dep org.junit.jupiter:junit-jupiter-api:6.0.3 \
src/test/java/shop/RepeatTest.java
error: src/test/java/shop/RepeatTest.java:13:5: the annotation org.junit.jupiter.api.RepeatedTest is not supported by the experimental Java subset yet
Among them: @RepeatedTest, @TestFactory, @TestInstance, @Timeout, @Tag, @Order,
extensions, an annotation of JUnit's on an annotation type (a composed annotation), a test
an interface declares, the elements the table above does not name (classes of
@ValueSource, from and to of @EnumSource, cleanup and factory of @TempDir), a
parameter of a @Test method that is no @TempDir and a parameter of a parameterized test
whose type is JUnit's (ArgumentsAccessor, TestInfo); the checks
of AssertJ that read a class's fields by reflection (usingRecursiveComparison,
hasFieldOrPropertyWithValue, extracting by a name), soft assertions and the
assertions of dates, files and futures. A test class is made with its constructor of no
parameter, and a static class in a test class is run only as a file's own class is.
rontolisp format refuses a .java file: it re-indents S-expressions.