Last updated: 2022-08-23
Adding Test Jars in Gradle for multi-module projects
Gradle offers native support for multi-module projects, which is perfectly suited for Spring Boot applications. Often some of the modules provide test data or helper classes that are needed in other parts of the application. In order to reference this test data in other modules, the following extension to the build script is necessary.
Extension of the build script
Let's assume we have a simple multi-module project with the modules "base" and "web". Here "base" contains the class TestData
with a method initTestUsers
in the "src/test/java" section of the module.
To use this method also in the tests of the "web" module, the following extension of the build script is required.
Extending the build.gradle
This provides a new task testJar
for all modules of our project, which creates a separate jar only with the test data and is executed after the testClasses
step. Also, we've added the new library as a dependency to "web" available only in the test context, so we can finally use our TestData
class.
IntelliJ not updating test classes
IntelliJ supports the resolution of test jars, but sometimes changes to test classes are not immediately recognized by the other modules. Instead, only outdated versions are provided.
Therefore it is recommended to start the Gradle task testJar
manually in IntelliJ after each change in the test data. This will update the library stored in the cache.
Executing the testJar task in IntelliJ
In the Free plan of Bootify, Spring Boot applications can be initialized with a custom database schema, REST API and frontend. The Professional plan also provides an option for multi-module projects - this includes the setup of the testJar
task to save valuable development time.