Skip to content

An utility class for creating Lighter spy Gradle Projects for test

License

Notifications You must be signed in to change notification settings

gmullerb/spy-project-factory

Repository files navigation

Spy Project Factory

license Download

This project offers an utility class for creating Lighter spy Gradle Projects for test.

This project is licensed under the terms of the MIT license.

Goals

Have a Gradle project for test with:

  • Mock Plugins and Mock PluginManager.
    • Without this, Project will appeal to the network in order to get and apply the plugins, this can be time consuming and if the project use some paid by time platform this could be inadequate.
  • Mock Logger.

Features

Offers a SpyProjectFactory class that has:

  • build() method builds a Gradle spy Project with Default builder:
    • pluginManager field will be mocked.
    • plugins field will be mocked.
    • logger field will be mocked.
    • name field will be Spy Project.
  • build(final ProjectBuilder builder) method builds a Gradle spy Project with provided builder:
    • pluginManager field will be mocked.
    • plugins field will be mocked.
    • logger field will be mocked.
  • builder field, that allows to build Project without mocking.
    • This way only 1 ProjectBuilder instance will exist in the test project.

Using/Configuration

Prerequisites

SpyProjectFactory class will be used when using the Gradle API, then:

  dependencies {
    compile gradleApi()
  }

This library make use of Mockito and is already set in the internal dependencies, therefore no need to add it to the project.

Dependency configuration

  1. Add repository:
  repositories {
    maven {
      url 'https://dl.bintray.com/gmullerb/all.shared.gradle'
    }
  }
  1. Add Test dependency:
  dependencies {
    testCompile 'all.shared.gradle:spy-project-factory:+'
  }

Use SpyProjectFactory utility class

Building a Spy Project with the Default Builder

  final Project spyProject = SpyProjectFactory.build();

Building a Spy Project with the Custom Builder

  final ProjectBuilder someBuilder = ProjectBuilder.builder()
    .withName(..);
  final Project spyProject = SpyProjectFactory.build(someBuilder);

Using the Spy Project

"Adding" a Plugin

Since getPluginManager and getPlugins will be mocked, adding the plugin must be simulated [1], this imply:

  • Must manually add any extensions the plugin adds and required by the test [2].
  • Must manually add any task the plugin adds and required by the test [2].
  spyProject.extensions.add(EXTENSION_NAME, someExtension)
  spyProject.tasks.create(TASK_NAME, someTask)

E.g:

  final CodeNarcExtension extension = new CodeNarcExtension(spyProject)
  spyProject.extensions.add('codenarc', extension)
  spyProject.tasks.create('codenarcMain', CodeNarc)

[1] The advantage is that the Project will not appeal the network to get the plugins, something that can be time consuming and if the project use some paid by time platform this could be inadequate.
[2] This can allow the mocking or spying of an extension or task.

Verifying
  verify(spyProject.logger)
    .debug(..)

Building a Non spy Project

With default builder values
    final Project someProject = SpyProjectFactory.build.build();
With custom builder values
    final Project someProject = SpyProjectFactory.build
      .withName(..)
      .withProjectDir(..)
      ..
      .build();

Extending/Developing

Prerequisites

  • Java.
  • Git (only if you are going to clone the project).

Getting it

Clone or download the project[1], in the desired folder execute:

git clone https://github.com/gmullerb/spy-project-factory

[1] Cloning a repository

Set up

  • No need, only download and run (It's Gradle! Yes!).

Building

  • To build it:

    • gradlew: this will run default task, or
    • gradlew build.
  • To assess files:

    • gradlew assessCommon: will check common style of files.
    • gradlew assessGradle: will check code style of Gradle's.
    • gradlew checkstyleMain: will check code style of Java's source files.
    • gradlew checkstyleTest: will check code style of Java's test files.
    • gradlew pmdMain: will check code style of Java's source files.
    • gradlew pmdTest: will check code style of Java's test files.
    • assemble task depends on these six tasks.
  • To test code: gradlew test

    • This task is finalized with a Jacoco Report.
  • To get all the tasks for the project: gradlew tasks --all

Folders structure

  /src
    /main
      /java
    /test
      /java
  • src/main/groovy: Source code files.
  • src/test/groovy: Test code files[1].

[1] Tests are done with JUnit and Mockito.

Documentation

  • CHANGELOG.md: add information of notable changes for each version here, chronologically ordered [1].

[1] Keep a Changelog

License

MIT License

Additional words

Don't forget:

  • Love what you do.
  • Learn everyday.
  • Learn yourself.
  • Share your knowledge.
  • Learn from the past, dream on the future, live and enjoy the present to the max!.

At life:

  • Let's act, not complain.

At work:

  • Let's give solutions, not questions.