[英]Untestable grails (2.5.4) service using @PostConstruct with Spock unit testing
我有一个服务,我想通过@PostConstuct
初始化,通过在Config.groovy
获取一些配置条目。
我还想检查这些条目是否已正确配置,并抛出异常,以便我看到应用程序配置错误。
在为这项服务编写单元测试时,我在Spock中走到了尽头。
Spock显然调用@PostConstruct
方法,但仅在共享服务实例上调用,然后在被测试的实例上执行您测试的任何实例方法。
这有一个不正常的副作用:
我的初始化代码失败,因为我无法添加setupSpec
来初始化共享实例,或者它在测试中的方法失败,因为实际上没有在该实例上设置配置。
这是我的服务:
package issue
import org.codehaus.groovy.grails.commons.GrailsApplication
import javax.annotation.PostConstruct
class MyService {
GrailsApplication grailsApplication
String property
@PostConstruct
void init() {
println "Initializing... ${this}"
property = grailsApplication.config.myProperty
//Enabling this business sanity check make the service untestable under Spock, because to be able to run, we need to initialize the configuration
// of the shared instance - PostConstruct is only called on the shared instance for some reason.
// But the execution of the method under test will not have the initialized property, because the service being executed is not the shared instance
if (property == "[:]") {
throw new RuntimeException("This property cannot be empty")
}
}
void doSomething() {
println "Executing... ${this}"
println(property.toLowerCase())
}
}
这是我的第一次测试:
package issue
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(MyService)
class MyServiceSpec extends Specification {
def setup() {
grailsApplication.config.myProperty = 'myValue'
}
void "It fails to initialize the service"() {
expect:
false // this is never executed
}
}
这是第二个测试:
package issue
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(MyService)
class MyServiceWithSharedInstanceInitializationSpec extends Specification {
//Initializing the shared instance grailsApplication lets the @PostConstruct work, but will fail during method test
//because the instance that was initialized is the shared instance
def setupSpec() {
grailsApplication.config.myProperty = 'myValue'
}
void "It fails to execute doSomething"() {
when:
service.doSomething()
then:
def e = thrown(NullPointerException)
e.message == 'Cannot invoke method toLowerCase() on null object'
service.property == null
}
}
有没有办法干净地做到这一点? 或者,我是否必须放弃我的单元测试并进行(较慢的)集成测试,以此来解决这个奇怪的问题?
你可以在这里看到我的完整grails应用程序:
https://github.com/LuisMuniz/grails-spock-issue-with-postconstruct
我的初始化代码失败,因为我无法添加setupSpec来初始化共享实例,或者它在测试中的方法失败,因为实际上没有在该实例上设置配置。
我的建议是简单地调用init方法,因为你正在测试方法的逻辑和功能,而不是@PostConstruct
是否有效,这似乎是最有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.