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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:08:27+00:00 2026-05-31T20:08:27+00:00

I use both Spring and Spock plugins in my current project. I wrote a

  • 0

I use both Spring and Spock plugins in my current project. I wrote a bunch of test cases in Spock. All are passing without any error (All are GREEN!). But the same tests are failing if I try to test the app after some time. I don’t why this happens. This is the code (one of my failing test):

def 'list action: 1 user'() { 

   setup: 
      mockDomain(User,[userInstance]) 

   expect: 
      controller.list() == [userInstanceList: [userInstance] , userInstanceTotal: 1] 
      params.max == 10 

   where: 
      userInstance = new User(username:"antoaravinth",password:"secrets") 
} 

I get a big error for this:

java.lang.NullPointerException: Cannot invoke method encodePassword() on null object 
        at mnm.schedule.User.encodePassword(User.groovy:34) 
        at mnm.schedule.User.beforeInsert(User.groovy:42) 
        at org.grails.datastore.gorm.events.DomainEventListener.invokeEvent(DomainEventListener.java:188) 
        at org.grails.datastore.gorm.events.DomainEventListener.beforeInsert(DomainEventListener.java:110) 
        at org.grails.datastore.gorm.events.DomainEventListener.onPersistenceEvent(DomainEventListener.java:73) 
        at org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener.onApplicationEvent(AbstractPersistenceEventListener.java:46) 
        at org.grails.datastore.mapping.engine.EntityPersister.cancelInsert(EntityPersister.java:227) 
        at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.executeInsert(NativeEntryEntityPersister.java:1321) 
        at org.grails.datastore.mapping.engine.NativeEntryEntityPersister$1.run(NativeEntryEntityPersister.java:698) 
        at org.grails.datastore.mapping.core.impl.PendingOperationExecution.executePendingOperation(PendingOperationExecution.java:33) 
        at org.grails.datastore.mapping.core.AbstractSession.flushPendingOperations(AbstractSession.java:322) 
        at org.grails.datastore.mapping.core.AbstractSession.flushPendingInserts(AbstractSession.java:314) 
        at org.grails.datastore.mapping.core.AbstractSession.flush(AbstractSession.java:237) 
        at org.grails.datastore.mapping.query.Query.flushBeforeQuery(Query.java:596) 
        at org.grails.datastore.mapping.query.Query.list(Query.java:562) 
        at org.grails.datastore.mapping.query.Query.singleResult(Query.java:606) 
        at org.grails.datastore.gorm.GormStaticApi.count_closure11(GormStaticApi.groovy:311) 
        at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:301) 
        at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34) 
        at org.grails.datastore.gorm.GormStaticApi.count(GormStaticApi.groovy:307) 
        at mnm.schedule.UserController.list(UserController.groovy:241) 
        at mnm.schedule.UserControllerSpec.list action: 1 user(UserControllerSpec.groovy:34)

The Domain class :

package mnm.schedule
import org.example.*;
class User extends SecUser {
    Profile profile
    String username
    String password
    static constraints = {
    username(unique:true,size:3..15, blank:false)
    password(blank:false)
    String toString() {
        this.username
    }

    static mapping = {
        cache true
    }
    protected void encodePassword() {
    password = springSecurityService.encodePassword(password)
}

Set<SecRole> getAuthorities() {
    SecUserSecRole.findAllBySecUser(this).collect { it.secRole } as Set
}

def beforeInsert() {
    encodePassword()
}

def beforeUpdate() {
    if (isDirty('password')) {
        encodePassword()
    }
}

}

The same test is passing at times and at time throwing this error. Whats wrong here? How can it pass at times and fail at times?

Thanks in advance.

  • 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-31T20:08:28+00:00Added an answer on May 31, 2026 at 8:08 pm

    Check this blog as an example of mocking springSecurityService in domain objects

    http://www.block-consult.com/blog/2011/08/17/inject-spring-security-service-into-domain-class-for-controller-unit-testing/ or check https://stackoverflow.com/a/9789619/206351

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

Sidebar

Related Questions

I want to use both ContextLoaderListener (so that I can pass Spring Beans to
I'm trying to get spring-security to work with a project where there is both
I need to use the Spring Security User, Role/Authority and the UserRole-join classes both
I have the need to use Spring .Net in a project and am exploring
I want to use spring with jsf in my project. So how can I
I recently came across spring surf project and Spring webscripts . Both these technologies
I've scourged the internet for an example that would use both spring-json and annotated
We can use both Spring config file OR a .properties file to store and
In Spring Framework (both for java and .net) i can use the following definition
I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both

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.