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 8409407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:54:06+00:00 2026-06-09T23:54:06+00:00

I have a service class defined as follows class MyService { def otherService; def

  • 0

I have a service class defined as follows

class MyService {

  def otherService;

  def performService( ) {
     Navigator.navigate( retrieveData, { otherService.doSomething(it) } );
  }
  def retrieveData = { Integer offset, Integer maxRows
     DomainObject.list(offset,maxRows);
  }
}

Navigator.navigate( Closure retriever, Closure processor ) {
   def offset=0;
   def batchsize=100;
   while( (data=retriever(offset,batchsize)) ) {
      if(data.size()==0) break;
      data.each { processor(it) }
      offset += data.size();
   }
}

In my tests, I would like to mock the retriveData to retrieve mocked objects

@TestFor(MyService)
class MyServiceSpec extends Specification {
  def "test retriever"() {
     given:
     service.otherService = Mock(OtherService);
     service.metaClass.retrieveData = { Integer offset, Integer maxRows ->
       if(offset==0) return [ Mock(DomainObject), Mock(DomainObject) ]
       else return []
     }
     when:
     service.performService();

     then:
     2 * otherService.doSomething(_);
  }
}

It turns out that the test is not picking up the mocked retrieveData, instead it always picks up the MyService.retrieveData closure, wondering what is the right approach to mock a closure method..

  • 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-09T23:54:08+00:00Added an answer on June 9, 2026 at 11:54 pm

    Seems that it’s because retrieveData is a field, not a method. Can you try:

    given:
       ...
       service.retrieveData = { Integer offset, Integer maxRows ->
         ...
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Spring noob here. I have an osgi service defined as follows in one of
I have a C# class defined as follows: public class GenericItem<T> { public List<T>
I have a non-service class which is defined as such: class A{ B b
I have a WCF Service defined as: [ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] public class Service
I have WCF service that return Json. Data contract defined below [DataContract] public class
Let's say I have some classes defined as follows: class Security { Boolean AuthenticateUser(String
I have my JSF Handler defined in request scope as follows public class JsfHandler
I have a WCF service defined as follows: Imports System.ServiceModel Imports System.ServiceModel.Web <ServiceContract()> Public
I have some Service class which is defined as InstanceContextMode.Single, and is well known
I have a service class which has overloaded constructors. One constructor has 5 parameters

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.