Alexander Kiefer
Alexander Kiefer

Reputation: 556

Spock Integration Test interaction not counted

I want to write an integration test for my Grails User Class.

I have a afterUpdate()method which calls the afterUpdate method in the userService which calls another service.

Now I want to verify that the call to the other service is done correctly. If I debug the code, the method is called correctly. but my test always fails and says the method isn't called:

    def "After Update for User"() {
        given:
        RedisService.metaClass.'static'.del = {String key -> true}
        User user = new User()
        user.save(flush: true)

        when:
        user.afterUpdate()

        then:
        1 * redisService.del(!null)
    }

I have tried different things like

then:
1 * user.userService.afterUpdate(!null)
1 * userService.redisService.del(!null)
1 * redisService.del(!null)

but they are all failing.

I allways get:

| Failure:  After Update for User(com.boxcryptor.server.RedisCacheIntegrationSpec)
|  Too few invocations for:

1 * user.userService.afterUpdate(!null)   (0 invocations)

Unmatched invocations (ordered by similarity):

None

Too few invocations for:

1 * redisService.del(!null)   (0 invocations)

Unmatched invocations (ordered by similarity):

None

Update:

the after Update Method is really simple:

def afterUpdate(User user) {
    deleteETagCache(user)
}

def deleteETagCache(User user) {
    redisService.del(user.id.toString())
}

Upvotes: 0

Views: 829

Answers (1)

Fran García
Fran García

Reputation: 2050

I think you are confusing overriding methods with metaClass magic and mocking a class or an object. I would try:

def "After Update for User"() {
    given:
        def redisService = Mock(RedisService)
    and:
        User user = new User()
        user.save(flush: true)
    and:
        user.redisService = redisService

    when:
        user.afterUpdate()

    then:
        1 * redisService.del(user.id.toString())
}

Also, I would create as a unit test, not as an integration test.

Upvotes: 2

Related Questions