A Gradle Case Study

Tutorial - Gradle SOAP - Features Revealed - Part 3


The sourceSets closure maps standard source directory structure to whatever the project requires. The layout here says that there is only one source directory for Java files, which is the generated directory populated by the wsimport task. Everything else in “src”, whether written in Java or in Groovy, is compiled by groovyc. The test closure is even more explicit – there are no directories for javac to use. Everything under the ‘tests’ directory is compiled by groovyc. That’s actually a good integration principle. The groovyc compiler knows all about Java source code, so let it compile both the Java and the Groovy sources. That way it can resolve any potential cross compilation issues for you. So far, all of the code in this article has been from the Gradle build file. For completeness, Listing 3 shows a class defining a conversion rate service.

Listing 3

import net.webservicex.Currency;
import net.webservicex.CurrencyConvertor 
import net.webservicex.CurrencyConvertorSoap 

class ConversionRate {
    CurrencyConvertorSoap stub = 
        new CurrencyConvertor().getCurrencyConvertorSoap()

    double getConversionRate(Currency from, Currency to) {
        return from == to ? 1.0 : stub.conversionRate(from, to)

Listing 4 below shows a simple Spock test to check the implementation

Listing 4

import net.webservicex.Currency;
import spock.lang.Specification;

class ConversionRateSpec extends Specification {
    ConversionRate cr = new ConversionRate()

    def "same currency should be rate of 1"() {
        double rate = cr.getConversionRate(Currency.USD, Currency.USD)

        rate == 1.0

    def "rate from USD to INR is > 1"() {
        cr.getConversionRate(Currency.USD, Currency.INR) >= 1 

Even if you’ve never seen a Spock test before, this should be pretty intuitive. The class extends the Specification class from Spock, which makes it a Spock test class. Each test has a def return type, followed by a string explaining its goal, and empty parentheses. The first test uses a when/then pair as a stimulus/response. The “then” block contains boolean conditions that are evaluated automatically, so no assert-based keyword is required.

Since the actual exchange rates change all the time, the second test picks two currencies that are guaranteed to satisfy the condition. At the time this article was written, there were about 51 INR for 1 USD. The boolean test is in an expect block, which works the same way the then block did in the previous test. To make the test work, one last change to the build file is required. Add the following line to the dependencies block.


testCompile 'org.spockframework:spock-core:0.5-groovy-1.8'

That will download the proper version of Spock, along with its dependencies (like JUnit), and now the build will execute the tests as well. Version 0.5 is current as of this writing. Feel free to try updating the version number to whatever is current at the time you run the example.


While the project that motivated this article involved a simple Groovy/Java client on a Microsoft web service, the real goal was to illustrate several aspects of Gradle development. Among them were creating a custom task, using a configuration based on an external Ant jar, working with multiple repositories, defining and configuring an Ant task, inserting it into the normal build process, ensuring that the task only ran when necessary, and showing how to map an alternative project layout to what Gradle expects. Hopefully some or all of these tasks will be helpful to you in the future. All of the source code for this article is available at a GitHub repository located here.


This article first appeared in Java Tech Journal: Gradle. You can find other articles from that issues and other issues here

Ken Kousen
Ken Kousen

What do you think?

JAX Magazine - 2014 - 03 Exclucively for iPad users JAX Magazine on Android


Latest opinions