Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在spock测试中使用redisService我需要清理redis数据库以进行设置和清理_Java_Grails_Redis - Fatal编程技术网

Java 如何在spock测试中使用redisService我需要清理redis数据库以进行设置和清理

Java 如何在spock测试中使用redisService我需要清理redis数据库以进行设置和清理,java,grails,redis,Java,Grails,Redis,我有一个在redis中存储值的类。 当我测试它时,它工作正常。如果我第二次运行它,它将失败,因为redis上已经有一个条目了。我想在运行测试之前和测试完成之后清理redis数据库,但我一直无法做到这一点 我正在运行Grails2.2.4 我已经尝试了@autowired注释,@mock(RedisService),也尝试了RedisService=grails.util.Holders.applicationContext.getBean('RedisService')作为RedisServic

我有一个在redis中存储值的类。 当我测试它时,它工作正常。如果我第二次运行它,它将失败,因为redis上已经有一个条目了。我想在运行测试之前和测试完成之后清理redis数据库,但我一直无法做到这一点

我正在运行Grails2.2.4

我已经尝试了
@autowired
注释,
@mock(RedisService)
,也尝试了
RedisService=grails.util.Holders.applicationContext.getBean('RedisService')作为RedisService
,但我无法创建RedisService的实例

这就是我想做的:

redisService.withRedis { Jedis redis ->
                redis.del("test")
            }
预期结果将是从redis中删除该条目

我遇到了以下几个错误: java.lang.NullPointerException:无法获取null对象上的属性“resource” 和 java.lang.IllegalArgumentException:ServletContext不能为null


任何帮助都将不胜感激。

在调用redis时,我似乎只需要使用注释@Mock(RedisService)和元类。

在调用redis时,我似乎只需要使用注释@Mock(RedisService)和元类。

如果你和我一样想知道我该怎么做:

@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
}

现在您可以测试您的服务了。

如果您像我一样想知道我该怎么做:

@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
}
现在您可以测试您的服务了