I use both Spring and Spock plugins in my current project. I wrote a bunch of test cases in Spock. All pass without errors (all GREEN!). But the same tests fail if I try to test the application after a while. I do not understand why this is happening. This is the code (one of my failed tests):
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)
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 passes from time to time and at the time this error is thrown. What is wrong here? How can he pass from time to time and fail?
Thanks in advance.
Ant's source share