Wednesday, November 11, 2015

How to test Gradle task's method using the Spock Framework ?

Yes,

I started to test Gradle as the new build system for my new job. It will replace the old custom build which is very hard to maintain or develop.

Why Gradle ? Because it's Java/Groovy based DSL ( Domain Specific Language ) with a great multiproject support an a possibility to plug in with your logic to many hooks.

I will write another post about my decision with some fair comparisons.

Today I wanted to extract the build logic and encapsulate it within a class task in the buildSrc directory.

The next step would be to use it in the script and use the Spock to test the inner works of my new class.

My method prepareCommandList has one File argument, so I needed some  temporary directory. I used a JUnit Rule for that.

Then it was a matter of instantiating the Task itself, you cannot just create a new object. You need a Gradle context with a use of Project and ProjectBuilder.

Then it comes to the Spock test itself, my idea was to process the file that is not have any valid information for the mapping. So it should be ignored by prepareCommandList. The code for that job is pasted below.