我想编写一些测试案例,其中我必须模拟配置文件的某些属性。
下面显示的是实际代码
if (password.length() < grailsApplication.config.user.password.min.length) {
return false
}
我想模拟grailsApplication.config.user.password.min.length
在我的配置文件中,user.password.min.length
已设置为6
我尝试通过以下方式进行模拟:
mockConfig = new ConfigObject()
mockConfig.user.password.min.length = 6
mockConfig("user.password.min.length")
mockConfig(user.password.min.length)
mockConfig(mockConfig.user.password.min.length)
但是,这些都不起作用。我的代码NullPointerException
有时会抛出。有人可以建议模拟配置文件的正确方法吗?
以下是我如何模拟服务规范中的配置属性。
@TestFor(MyService)
class MyServiceSpec extends Specification {
void "Test case for somemethod" () {
given:
grailsApplication.config.my.prop= 'some-prop-value'
when:
def result = service.getSomething(keyword)
then:
result.label.every {
it.toLowerCase().contains(keyword)
}
where:
keyword << ["dub", "sing"]
}
注意:这my.prop
是我正在使用内部getSomething(string)
方法的属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句