Git Product home page Git Product logo

java-module-testing's Introduction

Java Module Testing Gradle plugin

Build Status Gradle Plugin Portal

A Gradle 7.4+ plugin to turn a JVM Test Suite into a Blackbox or Whitebox Test Suite for Java Modules.

This plugin is maintained by me, Jendrik Johannes. I offer consulting and training for Gradle and/or the Java Module System - please reach out if you are interested. There is also my YouTube channel on Gradle topics.

If you have a suggestion or a question, please open an issue.

Java Modules with Gradle

If you plan to build Java Modules with Gradle, you should consider using these plugins on top of Gradle core:

Here is a sample that shows all plugins in combination.

Full Java Module System Project Setup is a full-fledged Java Module System project setup using these plugins.

How to use?

For a quick start, you can find some samples here:

For general information about how to structure Gradle builds and apply community plugins like this one to all subprojects you can check out my Understanding Gradle video series.

Plugin dependency

Add this to the build file of your convention plugin's build (e.g. build-logic/build.gradle(.kts) or buildSrc/build.gradle(.kts)).

dependencies {
    implementation("org.gradlex:java-module-testing:1.2.2")
}

Apply the plugin

In your convention plugin, apply the plugin.

plugins {
    id("org.gradlex.java-module-testing")
}

Blackbox Test Suites

The plugin automatically turns JVM Test Suites into Blackbox Test Suites if the src/<test-suite-name>/module-info.java file exists. A blackbox test suite is a separate module itself. See documentation on JVM Test Suites for more details on creating and configuring test suites.

Whitebox Test Suites

The plugin automatically turns JVM Test Suites without module-info.java file into Whitebox Test Suites. Whitebox Test Suites might require additional configuration, which can be done like this:

javaModuleTesting.whitebox(testing.suites["test"]) {
    requires.add("org.junit.jupiter.api")
    // opensTo.add("org.junit.platform.commons") <-- opensTo 'org.junit.platform.commons' is done by default
}

See documentation on JVM Test Suites for more details on creating and configuring test suites.

A whitebox test source set does not have a module-info.java. Instead, the main and test classes will be patched together and the test will run in the main module which now includes the test classes as well. Additional requires for the test are defined as shown above. If the sources under test are located in a different source set (not main), this can be configured via sourcesUnderTest.set("source-set-name").

What does the plugin do?

The plugin rewires the inputs of test compilation (:compileTestJava) and test runtime (:test). This includes configuring the Module Path and adding patch parameters in the case of whitebox testing.

Blackbox Test

Changes for test runtime (:test):

  • Normally, the test classes are loaded from a classes folder
  • Now, the test classes are packaged into a module jar together with the test resources. Otherwise, test resources would not be visible to the test module at runtime.

Whitebox Test

Changes for test compilation (:compileTestJava):

  • Normally, Gradle would not use the Module Path, as there is no moudle-info.java in the source set
  • Now, a Module Path is computed for the compilation. Using --patch-module, the test classes are compiled as an addition to the main module.

Changes for test runtime (:test):

  • Normally, Gradle would not run its test runner as Module, as there is no moudle-info.class as part of the compiled tests.
  • Now, the main and test classes are both used as locations for test discovery. By this, Gradle will find the moudle-info.class of the main module for the tests. Using --patch-module, main classes, main resources, test classes, and test resources folders are all merged to be treated as one module during test runtime.

Disclaimer

Gradle and the Gradle logo are trademarks of Gradle, Inc. The GradleX project is not endorsed by, affiliated with, or associated with Gradle or Gradle, Inc. in any way.

java-module-testing's People

Contributors

dependabot[bot] avatar jjohannes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.