How to use custom plugin extension from unit test


I’m writing my first standalone plugin that formats the project.version property according to if a buildNumber is passed via a custom extension or not.

I’m writing tests first and am using the ProjectBuilder to assist me, but can´t figure out how to add my custom extension.

Here is my plugin and extension code:

class VersionPluginExtension {
  String buildNumber

class VersionPlugin implements Plugin<Project> {
  void apply(Project project) {
    project.extensions.create("version", VersionPluginExtension)
    if (project.hasProperty('specificationVersion')) {
      def minorRevision = project.extensions.version.buildNumber ? project.extensions.version.buildNumber : InetAddress.getLocalHost().getHostName()
      project.version = project.getProperty('specificationVersion') + '.' + minorRevision
    } else {
      throw new PluginApplicationException('specificationVersion property does not exist')

and my test:

  void whenSupplyingABuildNumberThenProjectVersionShouldBeSetToSpecificationVersionConcatenatedWithBuildNumber() {
    def specificationVersion = '1.0'
    def buildNumber = '1'

    Project project = ProjectBuilder.builder().build()
    project.ext.specificationVersion = specificationVersion

    project.extensions.version.buildNumber = '1'

    assert project.version == constructVersion(specificationVersion, buildNumber)

  private String constructVersion(String specificationVersion, String buildNumber) {
    specificationVersion + '.' + buildNumber

This test fails since the extension is evaluated and applied before I set it in the test using project.extensions. Declaring it before applying the plugin does not work because the plugin creates it in apply(). So I found my self in a catch-22 situation.

There is very little information to be found on writing tests for plugins in the official documentation so I’m kinda stucked…

Anyone know how to achieve what I want?



I would use test kit for these kinds of tests.