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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:50:23+00:00 2026-05-12T16:50:23+00:00

I want to test a Grails controller which calls a service. I’d like to

  • 0

I want to test a Grails controller which calls a service. I’d like to mock the service. The Service has a method:

JobIF JobServiceIF.getJob(int)

and JobIF has a method:

String JobIF.getTitle()

Here’s my controller

def workActivities = {
   JobIF job = jobService.getJob(params.id)
   [career:job]
}

I understand that I need to mock the service and the job class (there are concrete implementations for both) but I’m struggling to get my head around the Groovy mocking object syntax. How do I mock a job and set the title to something, say “Architect” and then test the code?

So far I have:

void testWorkActivities() {
   def controller = new CareersController()
   ... // Mocking stuff I don't know how to do
   controller.params.id = 12
   def model = controller.workActivities()
   assertEquals "Architect", model["career"].getTitle()
}
  • 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-12T16:50:23+00:00Added an answer on May 12, 2026 at 4:50 pm

    You basically have two choices

    1. Use the Groovy mocking classes, i.e. MockFor and StubFor
    2. Use the Grails mock classes by calling the mockFor method of GrailsUnitTestCase. The class returned by this method is an instance of GrailsMock

    Personally, I have found the Groovy mock objects to a bit more reliable than the Grails mocks. On occasions, I’ve found that my Grails mock objects were bypassed, even though I appeared to be setting everything up correctly.

    Here’s an example of how to use the Groovy mocks:

    void testCreateSuccess() {
    
        def controller = new CareersController()
    
        // Create a mock for the JobService implementation class
        def mockJobServiceFactory = new MockFor(JobService)
    
        mockJobServiceFactory.demand.getJob {def id ->
            // Return the instance of JobIF that is used when the mock is invoked
            return new Job(title: "architect")
        }
    
        // Set the controller to use the mock service
        controller.jobService = mockJobServiceFactory.proxyInstance()
    
        // Do the test
        controller.params.id = 12
        def model = controller.workActivities()
        assertEquals "Architect", model["career"].getTitle()
    }
    

    The process is basically the same when using the Grails mocks, but you call the mockFor method of the test class, instead of instantiating MockFor.

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

Sidebar

Related Questions

I want to test a method defined in a rake task. rake file #lib/tasks/simple_task.rake
I want a unit-test framework for JavaScript which runs in the browser . Not
I want to test an ember controller is correctly adding a view to to
I want to make one-to-one relationship in grails and to TEST it. So i
I'm writing an integration test in Grails using GORM. I want to do something
I want to test whether My home / ends up in right controller and
I want to follow TDD, but the command grails test-app CUT needs almost a
We want test the JTAPI feature of our application. Are there any emulator for
I want test a FIX gateway for our company and was wondering if anything
I want to test whether an object is empty: {} . The following is

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.