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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:59:07+00:00 2026-05-22T21:59:07+00:00

first post here and hopefully relevant to many people. I’m working on writing integration

  • 0

first post here and hopefully relevant to many people.

I’m working on writing integration tests for a domain and on that domain I have a query using the withCriteria() method. I’ve searched all over the net and found many that give you detailed instructions on how to go about mocking a criteria query, but none on how to exactly test it.

I’ve tried mocking the domain using the mockDomain(domain,list) function, and setting up a domain for the test to use in the setUp() then calling the criteria and I get nothing.
I did a similar findBy here and got the results, but not exactly the ones I was looking for. I’m pretty sure it’s not just my query, but the criteria, I’ve read in a few places criteria does not work in service testing. The query thus far has worked for me in the app, but I want to have some tests that I can refer to later in case my code base changes.

I’ve actually done as many have suggested and pulled out the code for the query and made it a static method in my domain so that I can mock it for the tests that use it, but now I’m stuck with how to actually test this part. Do I need to run the app and just do functional testing from that standpoint, or is there some way I could do this in the grails unit/integration testing. I’ll post my query below.

static Attribute getDefinitionsUsingCriteria(List categoryNames, List types){
        def definitions = Definition.withCriteria() {
            and {
                'in'('type', types)
                if (categoryNames) {
                    categories {
                        'in'('name', categoryNames)
                    }
                }
            }
        }
        return definitions
    }

Definitions has a string property type, and has a property categories of type Set that each element in this set has a String name property.

I’m still pretty new to grails and have been reading many reference books, and I’m surprised this is missing in all of the books I’ve read thus far. I hope this is something that is just a mistake on my part, and easily testable. I appreciate any help, and thanks for reading this long post.

JR.

  • 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-22T21:59:08+00:00Added an answer on May 22, 2026 at 9:59 pm

    One way: move the test from test/unit to test/integration folder. Criteria won’t work in unit test (there’s no Hibernate there), but will in integration. Never use mockDomain() in integration tests.

    Note: don’t make the method static – it only complicates testing.

    Second way: In unit tests – use mockDomain(). Just rely on the fact that the logic is pretty straightforward, and unit-test everything except the method. Override it in setUp() like:

    Definition.metaClass.getDefinitionsUsingCriteria = { List categoryNames, List types ->
        delegate.findAll{ (it.type in types) && 
            (it.categories.find { c -> c in categoryNames }) 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First post here and it's and awk question. I have a file that looks
This is my first post here so kindly pardon any mistakes that I have.
I'm new to Java and this is my first post on here so hopefully
My first post here :) I have a summer job doing a bit of
This is my first post here. I have a problem. I need to take
Okay, my first post here so hopefully I don't screw up too badly and
First let me say that I have been working on this issue for about
First post here: -Deep Breath- In order to 'windowise' an app that works pretty
First post here, I have tried searching but couldn't find what I'm looking for
First post here! Ok.. I have a contact XML as follows: <contact> <item> <ContactData

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.