Search code examples
javamodulejunit5

Applying 1 junit-platform.properties file to entire project (multiple modules)


How can I apply a single junit-platform.properties to all modules in my project.

My project has 3 separate modules, each with its own test + resources.

Currently I'm duplicating and placing them in each resources directory. How can I reduce it to just 1 centralized junit-platform.properties and where should I place it?


Solution

  • The junit5 guide 4.5. Configuration Parameters describes

    The JUnit Platform configuration file: a file named junit-platform.properties in the root of the class path that follows the syntax rules for a Java Properties file.

    Therefore you could create a common project with your junit-platform.properties and add this project into the classpath for each of your projects.

    for Eclipse workspace

    Create baseproject with junit-platform.properties file.

    common baseproject with junit-platform.properties

    junit-platform.properties with custom conmfiguration e.g. junit.jupiter.displayname.generator.default

    junit.jupiter.displayname.generator.default = org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores
    

    Other project project2 without any own junit-platform.properties

    project2 without any own junit-platform.properties

    add baseproject to java build path of project2

    added baseproject to java build path of project2

    order and export of java build path of project2 with added baseproject

    Launching Junit test added baseproject into the classpath

    classpath of Junit Launcher with added baseproject

    Result of Junit test shows test display name with replaced underscores

    result of Junit test showing test display name with replaced underscores

    for IntelliJ workspace with gradle

    Using IntelliJ workspace , e.g. junitconfig. Add module baseproject for junit-platform.properties (as resource) Add module project2 for your Java files and Junit test files.

    Using IntelliJ workspace junitconfig

    Add module baseproject as dependency for module project2 in build.gradle

    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
        implementation project(':baseproject')
    }
    

    rebuild module project2 and run junit test

    result of Junit test showing test display name with replaced underscores