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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:52:16+00:00 2026-06-04T12:52:16+00:00

I stumbled across this when doing a refactoring with Grails 2.0.1 but I pulled

  • 0

I stumbled across this when doing a refactoring with Grails 2.0.1 but I pulled the basics of this problem out into a straight groovy 1.8.6 test and it still failed. I came across it because my method used to take no parameters and I changed it to take 1 parameter. When I changed the implementing production code none of my tests failed. Which is weird because the metaClassing that I had in the test was setup to accept no parameters but it was still responding to my production code when I passed in a parameter. So in the example below I’m wondering why the second metaClassing is being invoked and not the first. It doesn’t accept any parameters and as you can see I’m passing one in. If you switch the order of the metaClassing then it works appropriately but order shouldn’t matter in this case since the method signature is different. Any insight into why this is happening would be greatly appreciated.

import groovy.util.GroovyTestCase

class FirstTest extends GroovyTestCase {

    void testStuff() {
        def object = new Object()
        object.metaClass.someMethodName = {Object obj ->
            "ONE"
        }
        object.metaClass.someMethodName = {
            "TWO"
        }

        def result = object.someMethodName(new Object())

        assert "ONE" == result //result is equal to "TWO" in this case
    }

}

EDIT

Seems as if my above code may be more confusing than helpful so here is the actual code.

Original Production Code:

def create() {
    render(view: "create", model: [domains: Domain.myCustomListMethod().sort{it.cn}])
}

Original Test Code:

@Test
void createShouldIncludeAListOfAllDomainsInModel() {
    def directory = GldapoDirectory.newInstance(
            "", [
                    url: "http://url.com",
                    userDn: "someUserName",
                    password: "superSecretPassword"
            ])
    controller.session.userDirectory = directory
    Domain.metaClass.'static'.myCustomListMethod = {
        [[cn:"1"], [cn:"2"]]
    }

    controller.create()

    assert [[cn:"1"], [cn:"2"]] == controller.modelAndView.model.domains
}

I then updated the production code to pass in the session.userDirectory and my test still passed unmodified even though it’s not setup to receive any parameters:

def create() {
    render(view: "create", model: [domains: Domain.list(session.userDirectory).sort{it.cn}])
}
  • 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-04T12:52:17+00:00Added an answer on June 4, 2026 at 12:52 pm

    Closures by default take one parameter (of class Object), even if none is declared (accessible via the default variable it)

    So your second closure is overriding the first

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

Sidebar

Related Questions

I'm doing some refactoring work on PHP/MySQL code and stumbled across an interesting problem,
I stumbled across this problem in F#. Suppose, I want to declare two types
This is a problem one of our developers brought to me. He stumbled across
I recently stumbled across this problem while testing my C# program. parsing ) -
I stumbled across this before and I know it is a common problem. One
I'm refactoring some code that a friend wrote and recently stumbled across this function:
I stumbled across this youtube video here http://www.youtube.com/watch?v=Ha5LficiSJM that demonstrates someone doing color detection
I stumbled across this C code today. Can anyone tell me what the 'where'
I'm currently learning Ruby and RoR and I stumbled across this declaration: link_to_remote(name, options
I need to add indexes to my table (columns) and stumbled across this post:

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.