Enterprise Java

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.

Marcin Zajaczkowski

Marcin is an experienced architect who specializes in creating high quality software. Being under the impression of the Agile methodologies and the Software Craftsmanship movement, he believes in the value of good, testable and maintainable code. He aims to forge good software that makes the client delighted and the team proud of how the code itself looks. In his teaching, as a conference speaker, college lecturer, IT coach and trainer, he shows how to guide software development effectively using tests (with TDD, pair programming, Clean Code, design patterns, etc.) and maintaining a quality-oriented development environment (with CI, Sonar, automatic deployment, etc.). He is also a FOSS projects author and contributor, a Linux enthusiast.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button