Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spock and Roo = Easier Add-on Testing

DZone's Guide to

Spock and Roo = Easier Add-on Testing

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Hey, before I was a Roo in Action author, I was a huge fan of Grails. I still am, because Groovy + your favorite APIs is so much more concise to program against than with stock Java. So, one of my backburner projects was getting Groovy to work on Roo.

Roo doesn't like Groovy (yet)

It turns out the reason is AspectJ. Right now, our projects are all compiling against the AspectJ API (version 1.2.x if memory serves). The problem is that Groovy's bytecode is not compatible with AspectJ, at least in terms of getting it to compile in the same Maven compiler step. I looked on the AspectJ timeline, and saw AspectJ 1.5 and 2.0 will support Groovy as a target for weaving. Coolness!

But that doesn't stop us from using it to test!

Yep, that's right. Turns out you can attach the Groovy compiler to your test phase, and it works. By the time your tests are compiling, the main classes have been compiled and weaved with that AspectJ goodness. So your ITD methods are there, and you can use any old testing tool you want to test 'em.

Note, I think you may have issues with my approach, if you are going to use the DataOnDemand testing framework, but so far, it seems like it works well.

My example for the evening - testing with mocks for Add-on Commands

Ok, here's a practical example. My jQueryUI add-on command class delegates the isAvailable and install commands to the Operations Implementation class, JqueryuiOperationsImpl. I wanted to start working up the test coverage for this add-on, because right now I am failing my hudson build due to a lack of test coverage.

So, here is a Groovy Spock specification, which I named JQueryuiCommandsTest.groovy and placed in src/test/groovy (after installing the maven script I'll reference below it). I've even created a Spock Roo add-on for you to use to set this up in your own projects.

package org.sillyweasel.addons.jqueryui

class JQueryuiCommandsTest extends spock.lang.Specification {

    JqueryuiCommands commands
    JqueryuiOperations operations

    def setup() {
        operations = Mock()
        commands = new JqueryuiCommands();
        commands.operations = operations;
    }

    def "when calling availability we defer to implementation" () {
        when:
        boolean result = commands.isApiInstallCommandAvailable()

        then:
        1*operations.isInstalljQueryCommandAvailable()
    }

    def "when calling ui availability we defer to implementation" () {
        when:
        boolean result = commands.isInstalljQueryUIAvailable()

        then:
        1*operations.isInstalljQueryUICommandAvailable()
    }

    def "when calling installjQueryApi we defer to jquery operations method" () {
        when:
        commands.installjQueryApi()

        then:
        1*operations.installjQueryApi();
    }

    def "when calling installjQueryUI we defer to jquery operations method"() {
        when:
        commands.installjQueryUIApi()

        then:
        1*operations.installjQueryUIApi();
    }


}

How did you get this to work?

I basically stole the configuration for Spock and Maven, and tweaked it a bit. Here are the relevant bits of my Maven build:

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-core</artifactId>
    <version>0.6-groovy-1.8</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>0.6-groovy-1.8</version>
    <scope>test</scope>
</dependency>

...

<plugin>
   <groupId>org.codehaus.gmaven</groupId>
   <artifactId>gmaven-plugin</artifactId>
   <version>1.4</version>
   <configuration>
       <providerSelection>1.8</providerSelection>
       <source>src/test/groovy</source>
   </configuration>
   <executions>
       <execution>
           <goals>
               <goal>testCompile</goal>
           </goals>
       </execution>
   </executions>
   <dependencies>
       <dependency>
           <groupId>org.codehaus.gmaven.runtime</groupId>
           <artifactId>gmaven-runtime-1.7</artifactId>
           <version>1.3</version>
           <exclusions>
               <exclusion>
                   <groupId>org.codehaus.groovy</groupId>
                   <artifactId>groovy-all</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <dependency>
           <groupId>org.codehaus.groovy</groupId>
           <artifactId>groovy-all</artifactId>
           <version>1.8.6</version>
       </dependency>
       <dependency>
           <groupId>org.spockframework</groupId>
           <artifactId>spock-core</artifactId>
           <version>0.6-groovy-1.8</version>
       </dependency>
       <dependency>
           <groupId>org.spockframework</groupId>
           <artifactId>spock-spring</artifactId>
           <version>0.6-groovy-1.8</version>
       </dependency>
   </dependencies>
</plugin>

 

 

 

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}