Let's examine how this works by testing the packt.sortutil from the sample address book viewer application. The code is available at the 12-build-tools-and-testing/02-testing location. The src folder contains the packt.sortutil module--the module under test.
To test this, we can create a new test module: packt.sortutil.test. A good convention to follow is to name the test modules with the name of the module being tested followed by .test. Here's the module definition for packt.sortutil.test:
module packt.sortutil.test { requires packt.sortutil; }
By declaring the dependency on the module, you can access its exported types and test them through code. Here's a sample class in the test module that verifies that the output is accurate:
package packt.util.test; public class SortUtilTestMain { public static void main(String[] args) { SortUtil sortUtil = new SortUtil(); List out = sortUtil.sortList(Arrays.asList("b", "a", "c")); assert out.size() == 3; assert "a".equals(out.get(0)); assert "b".equals(out.get(1)); assert "c".equals(out.get(2)); } }
Compiling and running the code with assertions enabled (the -ea argument) tells us that our tests have passed:
$ javac -d out --module-source-path src --module
packt.sortutil,packt.sortutil.test $ java -ea --module-path out:lib --module
packt.sortutil.test/packt.util.test.SortUtilTestMain
You should not see any output, which indicates all assertions have successfully passed.