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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:20:36+00:00 2026-05-18T08:20:36+00:00

I am using groovy to create some mock classes for a test case. I

  • 0

I am using groovy to create some mock classes for a test case. I am basically creating dummy objects where all the methods return null so that i can run my testcase.

I am using the following syntax:

MessageFactory.instance = ["getMessage": {a,b,c,d -> "dummy"}] as MessageFactory

So here i am trying to overwrite the singleton instance with my on fake factory object. The problem is that MessageFactory’s constructor happens to be a private method. This gives me an illigal access exception when i run the code above. Is there a away i can create a proxy in groovy and overcome the private constructor issue?

  • 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-18T08:20:36+00:00Added an answer on May 18, 2026 at 8:20 am

    If you have access to the MessageFactory, and are willing to modify it, then you use the standard dependency-injection solution, as detailed here: mock singleton
    ..Though it’s not particularly Groovy.

    Otherwise, the best workaround I’ve found is to override the method(s) on the singleton instance itself, like so:

    @Singleton
    class Test{
        def method(){"Unmocked method called"}
    }
    
    
    def test = Test.instance
    test.metaClass.method = {-> null}
    
    test.method() // Now returns null
    

    Naturally, as a singleton, this instance doesn’t change (at least in theory)… So, overriding methods in this manner is effectively global.

    Edit: Or you can use GMock, which supports constructor mocking (among other things).

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

Sidebar

Related Questions

This is weird: using groovy strings to hold some command lines for execution, I
I'm trying to do some basic queries using the groovy.sql.Sql object, and I'm getting
I am performing some mass writing in a .csv file using Groovy. More specifically,
I am writing some scripts in Eclipse 2.7 RC4, using the latest Groovy Eclipse
Using groovy, would you expect better performance in terms of speed and memory overhead
I'm able to run Groovy scripts from Groovy using proc = cmd /c groovy
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
I'm using Ant to build some Java projects. In some, I've got a lib/
I'm using scaffolding for a couple of Controllers for two Domain Classes: 1 Sector
I'm trying to dynamically create domain objects in Grails and encountered the problem that

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.