PIT, JUnit 5 and Gradle – with just one extra line of configuration
Discover dead simple, improved PIT and JUnit 5 configuration in Gradle (with gradle-pitest-plugin 1.4.7+).
JUnit 5 is undeniably more and more popular nowadays. While there is a dedicated plugin for PIT for JUnit 5 and it has been supported by gradle-pitest-plugin for years, it was required to add a few lines of boilerplate code to achieve that. Recently, I’ve got a [question](https://github.com/szpak/gradle-pitest-plugin/issues/177) if it could be simplified. I liked it. Challenge accepted :-).
Generic approach with ‘buildscript {}’
First, take a look at the generic approach with the buildscrip {}
code block, which remembers times of Gradle 0.x:
buildscript { repositories { mavenCentral() gradlePluginPortal() //optionally, if any plugin is not available in Maven Central } configurations.maybeCreate('pitest') dependencies { classpath 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.4.6' pitest 'org.pitest:pitest-junit5-plugin:0.12' } } apply plugin: 'java' apply plugin: 'info.solidsoft.pitest' pitest { testPlugin = 'junit5' }
Just 3 extra lines. Acceptable, however buildscript {}
for plugin configuration is somehow verbose by itself.
Modern approach with ‘plugins {}‘
(with older gradle-pitest-plugin)
The modern variant with plugins {}
should be shorter:
buildscript { repositories { mavenCentral() } configurations.maybeCreate('pitest') dependencies { pitest 'org.pitest:pitest-junit5-plugin:0.12' } } plugins { id 'java' id 'info.solidsoft.pitest' version '1.4.6' } pitest { testPlugin = 'junit5' }
Unfortunately, the compact syntax of the plugin {}
block is wasted by a need to add an extra dependency pitest-junit5-plugin
used by gradle-pitest-plugin
in runtime in the buildscript {}
block – 10 lines extra. Highly disappointing ;-).
Modern improved approach with ‘plugins {}‘
and gradle-pitest-plugin 1.4.7+
With just released gradle-pitest-plugin
1.4.7 we can forget about all the boilerplate code:
plugins { id 'java' id 'info.solidsoft.pitest' version '1.4.7' } pitest { //adds dependency to org.pitest:pitest-junit5-plugin and sets "testPlugin" to "junit5" junit5PluginVersion = '0.12' }
Just one line junit5PluginVersion = '0.12'
which under the hood adds a pitest-junit5-plugin
dependency in the required versions and activates junit5
as testPlugin
used by PIT. Doesn’t it look nice? :-)
Summary
In this short blog post I presented how the configuration of the PIT, JUnit 5 and Gradle(-pitest-plugin) could be simplified with just a few changes in the plugin itself. All thanks to the question by John Scancella and the idea that sprang into my mind how to implement it in the smart way.
Therefore, I encourage you to report (sensible) ideas for improvements and things that limit you in the projects you use (or even better a pull/merge request after the initial discussion with the maintainer). Maybe it will implemented (accepted) for common good :-).
Published on Java Code Geeks with permission by Marcin Zajaczkowski, partner at our JCG program. See the original article here: PIT, JUnit 5 and Gradle – with just one extra line of configuration Opinions expressed by Java Code Geeks contributors are their own. |