This page looks best with JavaScript enabled

PIT, JUnit 5 and Gradle - with just one extra line of configuration

 ·  ☕ 3 min read

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 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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:

1
2
3
4
5
6
7
8
9
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 :-).

Share on

Marcin Zajączkowski
WRITTEN BY
Marcin Zajączkowski
Software Craftsman & Solution Architect
An experienced architect aiming for high quality solutions. Very engaged in evangelising Software Craftsmanship, Clean Code and Test-Driven Development as a conference speaker and a trainer. A specialist in Continuous Delivery and Continuous Inspection of Code Quality. An enthusiast of Reactive Systems and broadly defined concurrency.

Besides, open source author and contributor, a proud Linux user.


Comments

Not replies yet. Be the first!
Want to comment? Click here.
What's on this Page