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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:12:20+00:00 2026-05-22T17:12:20+00:00

I have some often-used helper methods for unit tests put into a seperate file.

  • 0

I have some often-used helper methods for unit tests put into a seperate file. The idea is to, for example, allow my XYZTests.groovy to call TestHelper.getUserObject() in order to get a fully initialized instance of User.

Now the problem is, that there’s a springSecurityService.encodePassword(pw) being called in the User’s beforeInsert() which always fails as there’s no mock for springSecurityService in TestHelper.groovy.

java.lang.NullPointerException: Cannot invoke method encodePassword() on null object

In User.groovy:

def beforeInsert() {
    // ...
    password = springSecurityService.encodePassword(pw)
    // ...
}

Note: I would like to avoid any mocking in TestHelper.groovy in order to use it’s methods in integration tests too.

In spite of that, even if I try to call a mockFor() anywhere in the TestHelper.groovy, I get an MME:

No signature of method: static myproject.TestHelper.mockFor() is applicable for argument types: (java.lang.Class, java.lang.Boolean) values: [class grails.plugins.springsecurity.SpringSecurityService, true]
groovy.lang.MissingMethodException: No signature of method: static myproject.TestHelper.mockFor() is applicable for argument types: (java.lang.Class, java.lang.Boolean) values: [class grails.plugins.springsecurity.SpringSecurityService, true]
    at myproject.TestHelper.mockSpringSecurityService(TestHelper.groovy:59)
    at myproject.TestHelper$mockSpringSecurityService.callStatic(Unknown Source)
    at myproject.TestHelper.getUserObject(TestHelper.groovy:47)
    at myproject.TestHelper$getUserObject.call(Unknown Source)
    at myproject.UserTests.setUp(UserTests.groovy:26)

Note: I currently mock the springSecurityService.encodePassword like this:

// in UserTests.groovy
protected void setUp() {
    // mockDomain(...) and such here

    def u = TestHelper.getUserObject("Pummel")
    u.springSecurityService = mockSpringSecurityService()
    assert u.save()
}
private mockSpringSecurityService() {
    def ssService = mockFor(SpringSecurityService,true)
    ssService.metaClass.encodePassword() { password ->
        "08a2d3c63bf9fc88276d97a9e8df5f841fd772724ad10f119f7e516f228b74c6"
    }

    ssService
}
  1. Any ideas on how I may be able to use the helper class while leaving all mocking in the unit tests only?
  2. Where would I best place the TestHelper.groovy file for using it in integration AND unit tests?

Note that everything is working perfectly fine when I move all helpers into UserTests.groovy directly!

  • 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-22T17:12:21+00:00Added an answer on May 22, 2026 at 5:12 pm

    The solution to this is to refrain from calling any user.save() in the TestHelper.groovy.

    This makes sense, as for many (unit) tests a persisted (saved) instance is unnecessary anyways.

    On the other hand many cases actually require an unsaved intance. (In order to test certain effects of the .save() itself, for example)

    A working example for integration tests would be:

    def user = TestHelper.getUserObject()
    user.save()
    

    For a unit tests:

    def user = TestHelper.getUserObject()
    user.springSecurityService = new SpringSecurityService() // or the described mock accordingly
    user.save()
    

    This keeps any mocks out of TestHelper.groovy

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

Sidebar

Related Questions

Often we have to use some of the methods which don't return anything useful.
I have an example of some code that I see often in websites that
I have a situation where I need to write some unit tests for some
Some controls have a useful context menu to access some often used properties, for
We have huge codebase and some classes are often used via reflection all over
I have some e-commerce code that I use often that uses Linq To SQL
We have a couple of sites which have some memory problems and fairly often
I have found that I often have to implement some sort of a scheduler
Often when writing PHP I'll have it output some HTML like this - echo
I often have a subroutine in Perl that fills an array with some information.

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.