HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

JUnit 5 with Gradle

By Lokesh Gupta | Filed Under: JUnit 5

Learn to configure junit 5 with gradle, its different modules and how to use them to create and execute tests.

Please note that JUnit 5 requires Java 8 at runtime.

1. JUnit 5 Gradle Dependency

To run JUnit 5 tests through gradle, you will need minimum two dependencies.

  1. JUnit Jupiter Engine Dependency

    JUnit jupiter is required to have two dependencies i.e. junit-jupiter-api and junit-jupiter-engine. junit-jupiter-api has junit annotations (e.g. @Test) to write tests and extensions and junit-jupiter-engine has test engine implementation which is required at runtime to execute the tests.

    Internally, junit-jupiter-engine is dependent on junit-jupiter-api, so adding junit-jupiter-engine only brings both dependencies into classpath.

    We can learn about internal dependencies between various junit jars in this image.

    dependencies {
    	testRuntime("org.junit.jupiter:junit-jupiter-engine:5.5.2")
            testRuntime("org.junit.platform:junit-platform-runner:1.5.2")
    }
    test {
        useJUnitPlatform()
    }
    
  2. JUnit Platform Runner Dependency

    We will need junit-platform-runner for executing tests and test suites on the JUnit Platform in a JUnit 4 environment.

    Internally, junit-platform-runner is dependent on junit-platform-suite-api and junit-platform-launcher, so adding junit-jupiter-engine only brings all three dependencies into classpath.

    testRuntime("org.junit.platform:junit-platform-runner:1.5.2")

2. Execute JUnit 4 Tests with JUnit 5

To execute junit 4 tests in junit 5 environment, you will need to include junit-vintage-engine dependency. JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.

dependencies {
    //To run JUnit 3 and junit 4 tests
    testCompile("junit:junit:4.12")
    testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
}

By configuring above in build.gradle, now you can run your old junit 3 or junit 4 tests with junit 5.

3. JUnit 5 Gradle Example

A sample build.gradle file for running tests built with junit 5 is as follow:

buildscript {
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
	}
}

repositories {
	mavenCentral()
}

ext.junit4Version        = '4.12'
ext.junitVintageVersion  = '4.12.2'
ext.junitPlatformVersion = '1.0.2'
ext.junitJupiterVersion  = '5.0.2'
ext.log4jVersion         = '2.9.0'

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.junit.platform.gradle.plugin'

jar {
	baseName = 'junit5-gradle-consumer'
	version = '1.0.0-SNAPSHOT'
}

compileTestJava {
	sourceCompatibility = 1.8
	targetCompatibility = 1.8
	options.compilerArgs += '-parameters'
}

junitPlatform {
	// platformVersion '1.0.2'
	filters {
		engines {
			// include 'junit-jupiter', 'junit-vintage'
			// exclude 'custom-engine'
		}
		tags {
			// include 'fast'
			exclude 'slow'
		}
		// includeClassNamePattern '.*Test'
	}
	// configurationParameter 'junit.jupiter.conditions.deactivate', '*'
	// enableStandardTestTask true
	// reportsDir file('build/test-results/junit-platform') // this is the default
	logManager 'org.apache.logging.log4j.jul.LogManager'
}

dependencies {
	// JUnit Jupiter API and TestEngine implementation
	testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
	testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")

	// If you also want to support JUnit 3 and JUnit 4 tests
	testCompile("junit:junit:${junit4Version}")
	testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")

	// To avoid compiler warnings about @API annotations in JUnit code
	testCompileOnly('org.apiguardian:apiguardian-api:1.0.0')

	// To use Log4J's LogManager
	testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}")
	testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}")

	// Only needed to run tests in an (IntelliJ) IDE(A) that bundles an older version
	testRuntime("org.junit.platform:junit-platform-launcher:${junitPlatformVersion}")
}

task wrapper(type: Wrapper) {
	description = 'Generates gradlew[.bat] scripts'
	gradleVersion = '4.3.1'
}

Ref : Git

Drop me your questions in comments section.

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

Search Tutorials

JUnit 5 Tutorial

  • JUnit 5 – Introduction
  • JUnit 5 – Test LifeCycle
  • JUnit 5 – @BeforeAll
  • JUnit 5 – @BeforeEach
  • JUnit 5 – @AfterEach
  • JUnit 5 – @AfterAll
  • JUnit 5 – @RepeatedTest
  • JUnit 5 – @Disabled
  • JUnit 5 – @Tag
  • JUnit 5 – Expected Exception
  • JUnit 5 – Assertions Examples
  • JUnit 5 – Assumptions
  • JUnit 5 – Test Suites
  • JUnit 5 – Gradle Dependency
  • JUnit 5 – Maven Dependency
  • JUnit 5 – Execute Test in Eclipse
  • JUnit 5 – Eclipse Test Templates
  • JUnit 5 vs JUnit 4

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz