Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8910675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:44:58+00:00 2026-06-15T03:44:58+00:00

I thought I’ve understood Spock interactions but I have to admin that I’m still

  • 0

I thought I’ve understood Spock interactions but I have to admin that I’m still missing some pieces of the picture.

Alright, here my problem: I have a method in a Grails service which performs some operations, including a call to a void method of the same service class. Here’s the code:

class myService {
    void myStuff(def myObj, params) {
        // do my stuff

        anotherMethod(myObj)

        //do my stuff again
    }

    void anotherMethod(myObj) {
        // do other stuff
    }
}

I want to be sure that myStuff method calls anotherMethod, to test and document the correct behaviour.

So, here’s the test in my Spock Specification class:

void 'test myStuff method'() {
    given: 'my object and some params'    
        // doesn't really matter what I'm doing here
        MyObject myObj = new MyObject()
        params = [title: 'my object']

    when: 'we call myStuff()'
        service.myStuff(myObj, params)

    then: 'anotherMethod() is called exactly one times'
        1 * service.anotherMethod(myObj)
    }
}

The error I get is:

Too few invocations for:

1 * service.anotherMethod(myObj)   (0 invocations)

What do you think? What’s wrong?

As always, thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:44:59+00:00Added an answer on June 15, 2026 at 3:44 am

    You are asking for a very special, and generally discouraged, form of mock called partial mocking where methods on the class under test are mocked. Spock supports this since 0.7, but you’ll have to create a Spy() rather than a Mock(). Also note that you cannot mock private methods. For more information on spies, see the reference documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought I'd find more about this topic but I didn't. I have to
I thought I had this all figured out, but now that I'm writing a
I thought I might restrict it to show only on some IPs, but I
I thought this would be pretty simple, but guess not. I have the following
I thought that :folddoc execute v:foldstart.p would work, but it only repeatedly returns the
Thought I will point out first that I have looked around on Stackoverflow and
I thought I understood the chaining power of jQuery, but now I'm a little
I thought this would be an easy thing but many hours have gone by
I thought I heard that py2exe was able to do this, but I never
I thought that there was some way in .net 3.0 to give an array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.