4

我在当前项目中同时使用 Spring 和 Spock 插件。我在 Spock 中写了一堆测试用例。所有都通过,没有任何错误(所有都是绿色的!)。但是如果我在一段时间后尝试测试应用程序,同样的测试会失败。我不明白为什么会这样。这是代码(我的失败测试之一):

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") 
} 

我对此有一个很大的错误:

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)

域类:

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()
    }
}

}

有时通过相同的测试并且有时会抛出此错误。这里有什么问题?它怎么会时而通过,时而失败?

提前致谢。

4

1 回答 1

4

查看此博客作为在域对象中模拟 springSecurityService 的示例

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

于 2012-03-23T15:17:02.093 回答