DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Spock and Roo: Easier Add-on Testing, Part 2

Spock and Roo: Easier Add-on Testing, Part 2

Ken Rimple user avatar by
Ken Rimple
·
Jun. 04, 12 · Java Zone · Interview
Like (0)
Save
Tweet
2.21K Views

Join the DZone community and get the full member experience.

Join For Free

Moving on to some more interesting tests. Given this method:

public boolean isInstalljQueryCommandAvailable() {
    String jsLocation = pathResolver.getFocusedIdentifier(
        Path.SRC_MAIN_WEBAPP, "/js");

    return fileManager.findMatchingAntPath(
        jsLocation + "**/jquery-1.*.min.js").isEmpty();
  }

I want to use Spock to test it. The challenge is the somewhat more nested set of objects. My add-on extends the AbstractOperations class (to get the embedded fileManager), so I need to mock that, plus mock the path resolver I've mounted with @Reference in my add-on as well.

To set it up I do this:

class JqueryuiOperationsImplTest extends spock.lang.Specification {

    JqueryuiOperationsImpl operations;

    def setup() {
        operations = new JqueryuiOperationsImpl();

        operations.pathResolver = Mock(PathResolver);
        operations.fileManager = Mock(FileManager);
    }

Spock mocks are similar to EasyMock, in that we then detail our assertions of what should happen before the test runs. In fact, based on a really interesting thread I found this AM while banging my head against the wall (don't do that, it hurts), if you put any mocking assertions in the when: part of a Spock test, it moves them to the setup: block. Anyway, here is my set of assertions:

def "isJqueryInstallAvailable called and happy path"() {

    setup:
    1* operations.pathResolver.getFocusedIdentifier(
            _, _) >> "src/main/webapp/js"

    1* operations.fileManager.findMatchingAntPath(
            _ as String) >> new TreeSet<FileDetails>()

I'm using Spock's matchers to eat the expressions - I don't really care what we pass to the getFocusedIdentifier or findMatchingAntPath methods, I just want them mocked and I want them to return values.

The >> is what tells us that we're stubbing the return output.

Here is the full test:

def "isJqueryInstallAvailable called and happy path"() {
    setup:
    1* operations.pathResolver.getFocusedIdentifier(
            _, _) >> "src/main/webapp/js"

    1* operations.fileManager.findMatchingAntPath(
            _ as String) >> new TreeSet<FileDetails>()

    when:
    def result = operations.isInstalljQueryCommandAvailable();

    then:
    result == true
}

Don't do what I did to get my head bruised. I originally wrote this:

1* operations.fileManager.findMatchingAntPath(
                _ as String).empty() >> false

I got my brain mixed up because I saw the line:

return fileManager.findMatchingAntPath(
    jsLocation + "**/jquery-1.*.min.js").isEmpty();

And that's going to throw a tasty NullPointerException because you're mocking the return of the method in fileManager, not the return statement! Oh, bother.

 

 

 

 

 

 

 

Spock (testing framework)

Published at DZone with permission of Ken Rimple, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Refactoring Java Application: Object-Oriented And Functional Approaches
  • Top Soft Skills to Identify a Great Software Engineer
  • Choosing Between GraphQL Vs REST
  • Ultra-Fast Microservices: When Microstream Meets Wildfly

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo