我的Grails集成测试套件(使用Spock)中有一些非常相似的测试。我想要一个具有90%的通用测试逻辑的基本测试类,然后让测试类从其扩展。
我刚在想:
public abstract BaseSpecification extends IntegrationSpec {
public baseTest() {
//
setUp:
//
...
when:
//
...
then:
...
}
}
然后:
public class SpecificTestSpecification extends BaseSpecification {
public baseTest() {
setup:
// more set up
super.baseTest();
when:
// some more specific testing
then:
// som more testing
}
}
但是尝试这个我遇到两个问题:
BaseClass
和SpecificationClass
SpecificationClass
时,它将失败:groovy.lang.MissingMethodException: No signature of method: BaseSpecification.baseTest() is applicable for argument types: () values: [] Possible solutions: any(), old(java.lang.Object), any(groovy.lang.Closure), notify(), wait(), Spy() at
有什么想法可以在我的Spock集成测试中实现继承吗?
最佳答案
我不知道用Spock是否可以做到。当我尝试时,我无法找到重用spock语句的方法,而我所做的就是编写一个可以在spock语句中使用的实用方法的BaseSpecification
类。
这是一个示例测试。
@TestFor(Address)
class AddressSpec extends BaseSpecification {
...
void "Country code should be 3 chars length"(){
when:
domain.countryCode = countryCode
then:
validateField('countryCode', isValid, 'minSize.notmet')
where:
[countryCode, isValid] << getMinSizeParams(3)
}
和BaseSpecification类
class BaseSpecification extends Specification {
// Return params that can be asigned in `where` statement
def getMinSizeParams(Integer size){[
[RandomStringUtils.randomAlphabetic(size - 1), false],
[RandomStringUtils.randomAlphabetic(size), true]
]}
// Make an assetion, so it can be used inside `then` statement
protected void validateField(String field, String code, Boolean shouldBeValid){
domain.validate([field])
if(shouldBeValid)
assert domain.errors[field]?.code != code
else
assert domain.errors[field]?.code == code
}
}
这是一个单元测试,但我认为它也应该与集成测试一起使用。
关于grails - Grails Spock集成测试中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24641793/