Gradle Unit Testing

Keywords: Gradle Java Junit

Gradle Unit Testing

We can perform unit testing tasks by adding Java plug-ins to Gradle. By default, all tests in the project will be executed. If we only want to test one of the classes, we can use the Java system property test.single as the name of the test. In fact, the model of the system property is taskName. Le, where taskName is the name of the unit test type in our project. Here's how we build unit tests

1. Create Gradle Project and add in the build.gradle configuration file:

	// File: build.gradle
	apply plugin: 'java'
	repositories {
	    mavenCentral()
	}
	dependencies {
	    testCompile 'junit:junit:[4,)'
	}	
	test {
	    testLogging {
	        // Show that tests are run in the command-line output
	        events 'started', 'passed'
	    }
	}

2. The second step is to create a test class, one test method for each test class, so that we can call them separately later.

	// File: src/test/java/com/mrhaki/gradle/SampleTest.java
	package com.mrhaki.gradle;
	
	import static org.junit.Assert.*;
	import org.junit.*;
	
	public class SampleTest {
	
	    @Test public void sample() {
	        assertEquals("Gradle is gr8", "Gradle is gr8");
	    }
	    
	}
	
	// File: src/test/java/com/mrhaki/gradle/AnotherSampleTest.java
	package com.mrhaki.gradle;
	
	import static org.junit.Assert.*;
	import org.junit.*;
	
	public class AnotherSampleTest {
	
	    @Test public void anotherSample() {
	        assertEquals("Gradle is great", "Gradle is great");
	    }
	}

3. In order to execute only the test methods in the SampleTest class, we must execute unit tests from the command line with the Java system property - Dtest.single=Sample.

$ gradle -Dtest.single=Sample test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test

com.mrhaki.gradle.SampleTest > sample STARTED

com.mrhaki.gradle.SampleTest > sample PASSED

BUILD SUCCESSFUL

Total time: 11.404 secs

4. Note that only one test class is executed now. Gradle will use the query to query the test method in the class of **/<Java system property value=Sample>* mode, so we don't need to write the full class name of the test class. In order to execute only the AnotherSampleTest class, we do the same:

$ gradle -Dtest.single=AnotherSample test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test

com.mrhaki.gradle.AnotherSampleTest > anotherSample STARTED

com.mrhaki.gradle.AnotherSampleTest > anotherSample PASSED

BUILD SUCCESSFUL

Total time: 5.62 secs

5. We can also use the pattern of Java system attributes to match multiple test classes to execute multiple test methods at one time. For example, we can use * Sample to execute unit tests of the SampleTest and NotherSampleTest classes once.

$ gradle -Dtest.single=*Sample test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test

com.mrhaki.gradle.AnotherSampleTest > anotherSample STARTED

com.mrhaki.gradle.AnotherSampleTest > anotherSample PASSED

com.mrhaki.gradle.SampleTest > sample STARTED

com.mrhaki.gradle.SampleTest > sample PASSED

BUILD SUCCESSFUL

Total time: 5.605 secs

6. To prove that Java's system attributes also work for other test types, we added a new task in the buile.gradle configuration file. We called it sampleTest and included our test files. We also used testLogging to facilitate the output of test results in the console.

// File: build.gradle
apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:[4,)'
}

task sampleTest(type: Test, dependsOn: testClasses) {
    include '**/*Sample*'
}

tasks.withType(Test) {
    testLogging {
        events 'started', 'passed'
    }
}

7. Next we want to run the unit test of the SampleTest class. Now we can use - DsampleTest.single=S* as the running parameter.

$ gradle -DsampleTest.single=S* sampleTest
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:sampleTest

com.mrhaki.gradle.SampleTest > sample STARTED

com.mrhaki.gradle.SampleTest > sample PASSED

BUILD SUCCESSFUL

Total time: 10.677 secs
Code written with Gradle 1.6

If you are interested in discussing problems with us, you may join our group. Remember to write my name ID: Big MoreKT: 984370849 when you join the group.

Posted by Saruman on Tue, 17 Sep 2019 20:04:57 -0700