The standard way to test code in Java ecosystem is JUnit. The standard way to harness JUnit by Maven is the surefire plugin. Usually one doesn’t need to do anything to turn the plugin on - the standard Maven jar packaging does it automatically. However, it is possible to do magic with the plugin configuration and that is what this post is about.
JUnit Browser Intermezzo
However, before we dwell into the plugin configuration, let us set the context
by introducing the DukeScript JUnit extension -
it allows one to write the code once and then test it in different
DukeScript environments. Where DukeScript environment is a basically
a JVM configured to render HTML
in some way.
With a simple
@RunWith annotation your code can be executed multiple times,
in multiple different setups.
Where’s the problem? By default the JUnit Browser extension runs all the tests in a single Java virtual machine. Yet, some of the environments don’t go along well - for example the webkit presenter is using different GTK version than the default Apache JavaFX presenter. When used together - they crash the JVM.
Matrix of Runs
We need to run the surefire plugin multiple times. How can one do that? One needs to configure multiple test executions:
and the code is compiled and tested twice quickly. While one can schedule full verification with
Configuring the Matrix
The configuration section of each test run needs to alter the setup somehow - each of the runs is supposed to test slightly different environment, right? One can do that by setting different JVM properties, but our example has chosen a different route: it uses different classpath for each execution!
By default we add all three presenters to the project test classpath:
This makes sure the Maven downloads the necessary artifacts and places them
into the local repository. Then, each test execution masks all but one of the
presenters using builtin surefire plugin
Test runs are isolated in their own JVM and combination of environments no longer negatively influence each other.
Where's the Matrix?
Nice, but where is the matrix? may be your next question! Well, the above example
is taken from the DukeScript definitelytyped
There are hundreds of libraries ready and all of them share the same testing
setup. As such the configuration of the surefire plugin is done in the
By extending the configuration of the surefire plugin in the master
we can enlarge the axis of additional DukeScript environments. By including
surefire testing matrix grows with every new commit. Try it, contribute too:
Convert your own library! It is easy, read more, fork and pull request.