Running JUnit 5 in Gradle Project
Follow the given step to setup JUnit 5 in Gradle project. Newbies wanna read Setup first gradle project in eclipse.
Step 1. Prepare build.gradle for JUnit 5
Step 1.1. Attach JUnit 5 gradle plugin to project.
Properties are self-explanatory.
Complete build.gradle
This is how your build.gradle file will look like after following the above steps.
Step 2. Refresh dependencies
Refresh Gradle project so it'll download required libraries.
Step 3. Project structure.
Create test source folder parallel to main source folder (i.e src/test/java and src/test/resources). Follow the image.
Step 4. Create first unit test.
Source code (Addition.java)
I created two test cases. Out of two case one is success and other is fail.
Step 5. Clean and Test
Perform clean and test task on your gradle project.
Output
Step 1. Prepare build.gradle for JUnit 5
Step 1.1. Attach JUnit 5 gradle plugin to project.
apply plugin: 'org.junit.platform.gradle.plugin' buildscript { repositories { mavenCentral() } dependencies { // Gradle plugin classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3' } }Step 1.2. Add JUnit 5 dependencies.
dependencies { // download library for JUnit compile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M3' compile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M3' compile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-M3' // compile project for testing using Junit Jupiter Api testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3") }Step 1.3. JUnit 5 configuration.
Properties are self-explanatory.
junitPlatform { platformVersion "1.0.0-M3" filters { engines { include 'junit-jupiter' // exclude 'junit-vintage' } tags { // include 'fast', 'smoke' // exclude 'slow', 'ci' } packages { // include 'com.sample.included1', 'com.sample.included2' // exclude 'com.sample.excluded1', 'com.sample.excluded2' } //includeClassNamePattern '.*Spec' includeClassNamePatterns '.*Test', '.*Tests' } }
Complete build.gradle
This is how your build.gradle file will look like after following the above steps.
apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.junit.platform.gradle.plugin' sourceCompatibility = 1.8 repositories { mavenCentral() } buildscript { repositories { mavenCentral() } dependencies { // Gradle plugin classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3' } } dependencies { // download library for JUnit compile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M3' compile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M3' compile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-M3' // compile project for test using Junit Jupiter Api testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3") } junitPlatform { platformVersion "1.0.0-M3" filters { engines { include 'junit-jupiter' // exclude 'junit-vintage' } tags { // include 'fast', 'smoke' // exclude 'slow', 'ci' } packages { // include 'com.sample.included1', 'com.sample.included2' // exclude 'com.sample.excluded1', 'com.sample.excluded2' } //includeClassNamePattern '.*Spec' includeClassNamePatterns '.*Test', '.*Tests' } }
Step 2. Refresh dependencies
Refresh Gradle project so it'll download required libraries.
Step 3. Project structure.
Create test source folder parallel to main source folder (i.e src/test/java and src/test/resources). Follow the image.
Step 4. Create first unit test.
Source code (Addition.java)
package com.javaquery.math; public class Addition { /** * Add given number 'a' and 'b'. * @param a * @param b * @return */ public int add(int a, int b){ return a + b; } }Source code (AdditionTest.java)
I created two test cases. Out of two case one is success and other is fail.
package com.javaquery.math; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; import static org.junit.jupiter.api.Assertions.assertEquals; @RunWith(JUnitPlatform.class) public class AdditionTest { @Test public void add(){ int x = new Addition().add(12, 3); assertEquals(x, 15); } @Test public void failTest(){ int y = new Addition().add(20, 30); assertEquals(y, 20); } }
Step 5. Clean and Test
Perform clean and test task on your gradle project.
clean test
Output
[sts] ----------------------------------------------------- [sts] Starting Gradle build for the following tasks: [sts] clean [sts] test [sts] ----------------------------------------------------- :clean :compileJava :processResources UP-TO-DATE :classes :compileTestJava :processTestResources UP-TO-DATE :testClasses :junitPlatformTestJan 01, 2017 11:48:00 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines INFO: Discovered TestEngines with IDs: [junit-jupiter] Failures (1): JUnit Jupiter:AdditionTest:failTest() MethodSource [className = 'com.javaquery.math.AdditionTest', methodName = 'failTest', methodParameterTypes = ''] => org.opentest4j.AssertionFailedError: expected: <50> but was: <20> Test run finished after 105 ms [ 2 containers found ] [ 0 containers skipped ] [ 2 containers started ] [ 0 containers aborted ] [ 2 containers successful ] [ 0 containers failed ] [ 2 tests found ] [ 0 tests skipped ] [ 2 tests started ] [ 0 tests aborted ] [ 1 tests successful ] [ 1 tests failed ] :junitPlatformTest FAILED
0 comments :