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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:42:41+00:00 2026-05-23T23:42:41+00:00

In one of my unit tests, I am having some difficulty getting a mocked

  • 0

In one of my unit tests, I am having some difficulty getting a mocked method to executed. I have the following test code:

void testExample() {
    def mockICFService = new MockFor(ICFService)
    ...

    //Mock the methods
    controller.metaClass.icfList = { def person ->
        println "icfList"
        return [new IC(conceptId:'12345')]
    }
    mockICFService.demand.getAllIC(1..1) { def id, def withHist, def appId ->
        println "mocking service"
        return new Person()
    }
    ...

    def model = controller.detail()
}

Inside of detail in my controller class I create a Person via the ICFService’s getAllIC(). This part works correctly. Later in the function, however, there is a call to icfList (which is defined in the controller). Through println’s I have determined that the call is still being made, although it is returning an empty array. I believe that this is because the array is populated based on data in the servletContext, but in Unit Testing there is no access to that (hence my trying to mock it out).

Does anyone know how to force the test to use the mocked version of controller.icfList instead of calling the actual method in controller?

  • 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-05-23T23:42:42+00:00Added an answer on May 23, 2026 at 11:42 pm

    When I try your code, what blows up for me is the mocked service, and the part that works properly is the mocked-out icfList() method. The opposite of your observation, interestingly. For what it’s worth, here’s what I did:

    First replace new MockFor() class instantiation with the mockFor() method. Then you need to inject the mock service into the controller.

    def mockICFService = mockFor(ICFService)
    controller.iCFService = mockICFService.createMock()
    

    By doing the above, only the mocked versions of icfList() and getAllIC() get called, so you are not using the servletContext at all. Check out the Grails testing documentation for more info.

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

Sidebar

Related Questions

I have been having some problems with the test debugger in VS10. All tests
I'm having some troubles getting my unit tests to be setup to use an
Should one create unit tests involving IO? Ie, testing a class method for serializing/deserializing
I'm getting NoPrimaryKeyException when I try to run one of my unit tests which
Using Visual Studio 2008 for a unit test, one of the unit tests has
I have two Controller unit tests and each one sets an HttpClient metaclass execute
I'm having some difficulty unit testing my NHibernate repository methods, basically due to the
I have a situation where I need to write some unit tests for some
I have unit tests to run my code as follows: MyActivity myActivity = new
I have some unit tests that run fine on my dev machine on both

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.