I have a class that stores a value in redis. When i test it, it works properly. If i run it for the second tome it will fail as there is an entry on redis already. I want to cleanup the redis database before running the test and after it is finished but i haven't been able to do that.
I am running grails 2.2.4.
I have tried the @autowired
annotation, @mock(RedisService)
, also redisService = grails.util.Holders.applicationContext.getBean('redisService') as RedisService
and i am not able to create an instance of the redisService.
This is what i am trying to do:
redisService.withRedis { Jedis redis ->
redis.del("test")
}
The expected result would be that the entry is deleted from redis.
I have gotten several errors like: java.lang.NullPointerException: Cannot get property 'resource' on null object and java.lang.IllegalArgumentException: ServletContext must not be null
Any help would be appreciated.
In case you are like me wondering how exactly am I going to do that:
@Mock(RedisService) // above class definition
def setup(){
def redisPoolMock = new Object()
redisPoolMock.metaClass.getResource = { ->
throw new JedisConnectionException('Generated by a mocked redisPool')
// Or you can put the value in here maybe
}
redisService = new RedisService()
redisService.redisPool = redisPoolMock
service.redisService = redisService
}
And now you can test your service.
似乎我只需要在调用 redis 时使用注释 @Mock(RedisService) 和 metaClass 。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.