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

  • Home
  • SEARCH
  • 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 3321522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:04:22+00:00 2026-05-17T23:04:22+00:00

have a Grails domain object that has a custom static function to grab data

  • 0

have a Grails domain object that has a custom static function to grab data from the database

class Foo {
    /* member variables, mapping, constraints, etc. */

    static findByCustomCriteria(someParameter, List listParameter) {
        /* code to get stuff from the database... */

        /*
            Return value is a map
            ["one": "uno", "two": "due", "three": "tre"]
        */
    }

}

The static function findByCustomCriteria uses createCriteria() to build the query that pulls data from the Foo table, which means mockDomain(Foo) does not work properly when unit testing. What I’m trying to do to work around this is use one of the general purpose methods of mocking to mock out findByCustomCriteria, but I can’t get the syntax quite right.

I have a controller BarController that I’m trying to test, and buried in the call to BarController.someFunction() there is a call to Foo.findByCustomCriteria().

class BarControllerTest extends ControllerUnitTestCase {

    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSomeFunction() {

        /* Mocking for Foo goes here */

        assertEquals("someValue", controller.someFunction())
    }
}

What would be a way to mock this out?

I’ve tried using new MockFor(), mockFor(), and metaClass, but I can’t get it to work.


Edit:

Every time I tried to mock this out, I tried to mock it like so…

Foo.metaClass.'static'.findByCustomCriteria = { someParam, anotherParam ->
    ["one": "uno", "two": "due", "three": "tre"]
}

I guess I didn’t include enough information initially.

  • 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-17T23:04:22+00:00Added an answer on May 17, 2026 at 11:04 pm

    I’ve encountered this scenario more than once, you need to modify the static metaClass of Foo:

    Foo.metaClass.'static'.findByCustomCriteria = { someParameter, List listParameter ->
        ["one": "uno", "two": "due", "three": "tre"]
    }
    

    Typically I’ll put it in the test setup, so I don’t forget when it needs to be applied.

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

Sidebar

Related Questions

My Grails app has the following domain objects class ProductType { String name static
In my Grails domain I have something like the following class A { String
I've created a domain class in Grails like this: class MyObject { static hasMany
I have a grails project that contains a few domain objects. I am using
If I have a List in a Grails domain class, is there a way
I have a couple of domain classes defined, Employee and EmployeeDesiredSkill, Employee has static
How do I create a grails war file so that it doesn't have the
We have an MTOM-enabled web service that is published with Grails and the Metro
i have a class class Foo { BigInteger phase BigDecimal amount BigDecimal percent }
I'm working on a grails legacy project. A domain class called User exists. It

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.