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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:29:46+00:00 2026-05-23T11:29:46+00:00

EDIT: Please let me be clear, I’m asking how to do this in Grails

  • 0

EDIT: Please let me be clear, I’m asking how to do this in Grails using Spring Dependency Injection, and NOT Grails’ metaclass functionality or new().

I have a grails service that is for analyzing log files. Inside the service I use the current time for lots of things. For unit testing I have several example log files that I parse with this service. These have times in them obviously.

I want my service, DURING UNIT TESTING to think that the current time is no more than a few hours after the last logging statement in my example log files.

So, I’m willing to this:


class MyService {
    def currentDate = { -> new Date() }

    def doSomeStuff() {
        // need to know when is "right now"
        Date now = currentDate()
    }
}

So, what I want to be able to do is have currentDate injected or set to be some other HARDCODED time, like

currentDate = { -> new Date(1308619647140) }

Is there not a way to do this with some mockWhatever method inside my unit test? This kind of stuff was super easy with Google Guice, but I have no idea how to do it in Spring.

It’s pretty frustrating that when I Google “grails dependency injection” all I find are examples of


class SomeController {
  // wow look how amazing this is, it's injected automatically!!
  // isn't spring incredible OMG!
  def myService
}

It feels like all that’s showing me is that I don’t have to type new …()

Where do I tell it that when environment equals test, then do this:


currentDate = { -> new Date(1308619647140) }

Am I just stuck setting this property manually in my test??

I would prefer not to have to create a “timeService” because this seems silly considering I just want 1 tiny change.

  • 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-23T11:29:47+00:00Added an answer on May 23, 2026 at 11:29 am

    Thanks for the help guys. The best solution I could come up with for using Spring DI in this case was to do the following in

    resources.groovy

    These are the two solutions I found:

    1: If I want the timeNowService to be swapped for testing purposes everywhere:

    import grails.util.GrailsUtil
    
    // Place your Spring DSL code here
    beans = {
        if (GrailsUtil.environment == 'test') {
            println ">>> test env"
            timeNowService(TimeNowMockService)
        } else {
            println ">>> not test env"
            timeNowService(TimeNowService)
        }
    }
    

    2: I could do this if I only want this change to apply to this particular service:

    import grails.util.GrailsUtil
    
    // Place your Spring DSL code here
    beans = {
    
        if (GrailsUtil.environment == 'test') {
            println ">>> test env"
            time1(TimeNowMockService)
        } else {
            println ">>> not test env"
            time1(TimeNowService)
        }
        myService(MyService) {
            diTest = 'hello 2'
            timeNowService = ref('time1')
        }
    }
    

    In either case I would use the service by calling
    timeNowService.now().
    The one strange, and very frustrating thing to me was that I could not do this:

    import grails.util.GrailsUtil
    // Place your Spring DSL code here
    beans = {
        if (GrailsUtil.environment == 'test') {
            println ">>> test env"
            myService(MyService) {
                timeNow = { -> new Date(1308486447140) }
            }
        } else {
            println ">>> not test env"
            myService(MyService) {
                timeNow = { -> new Date() }
            }
        }
    }
    

    In fact, when I tried that I also had a dummy value in there, like dummy = 'hello 2' and then a default value of dummy = 'hello' in the myService class itself. And when I did this 3rd example with the dummy value set in there as well, it silently failed to set, apparently b/c timeNow blew something up in private.

    I would be interested to know if anyone could explain why this fails.

    Thanks for the help guys and sorry to be impatient…

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

Sidebar

Related Questions

I'm not sure what kind of terminology to use in this so please edit
Please let me know if you have any idea about it. Thanks EDIT What
A website I'm working on is using a third-party assembly, let's say A.dll. This
EDIT: ok let me be a bit more clear i have a html file
I am a newbie in magento. Please let me know how can I edit
This is somewhat a two-part question (please let me know if they should be
Hope my title is not too confusing. Please let me know if there is
Please note the Edit below for a lot more information, and a possible solution
**EDIT: There are several options below that would work. Please vote/comment according to your
Why new()/delete() is slower than malloc()/free()? EDIT: Thanks for the answers so far. Please

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.