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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:45:58+00:00 2026-06-02T11:45:58+00:00

I am writing a test for methodA() in a service class similar to the

  • 0

I am writing a test for methodA() in a service class similar to the one given below.

Class SampleService {
  def methodA(){
     methodB()
  }

  def methodB(){
  }
}

When I test methodA(), I need to be able to mock the call to methodB() when testing methodA(). I am using version 2.0.x of grails. In the 1.3.x distributions, I would write a self mock like this

def sampleServiceMock = mockFor(SampleService) 
sampleServiceMock.demand.methodB { -> } 

But this doesn’t work in the 2.0.x versions. I was wondering what are the other ways of mocking methodB() when testing methodA()

  • 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-02T11:45:59+00:00Added an answer on June 2, 2026 at 11:45 am

    For this kind of problem I actually avoid mocks and use the built-in groovyProxy ability to cast a map of closures as a proxy object. This gives you an instance with some methods overridden, but others passed through to the real class:

    class SampleService {
        def methodA() {
            methodB()
        }
    
        def methodB() {
            return "real method"
        }
    }
    
    def mock = [methodB: {-> return "mock!" }] as SampleService
    
    assert "mock!" == mock.methodA()
    assert "real method" == new SampleService().methodA()
    

    I like that only changes an instance, can be done in a single line, and doesn’t mess with the metaclass of anything outside of that instance that needs to be cleaned up.

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

Sidebar

Related Questions

this is my actual class I am writing test class below. public class ReadFile
I am writing a diagnostic site to test our server methods. One of the
I writing test cases for Android apps using Robotium. One of my test cases
I'm writing UI to test an asmx web service. Server and client are .NET.
I'm writing a functional test for an action that uses Symfony2's session service to
I'm writing JUnit test code for an android service implementation, and I have to
I am writing a test web service, and noticed a strange corner case. If
Iam writing a test method (in a .Net Test Project) and i need to
I am writing a test for my email service, inside my email template file
I am writing a simple web service using .NET, one method is used to

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.