grailsspock集成测试中的继承

grailsspock集成测试中的继承,grails,spock,Grails,Spock,我的Grails集成测试套件(使用Spock)中有一些非常类似的测试。我希望有一个基本测试类,它拥有90%的测试通用逻辑,然后让测试类从中扩展 我在想: public abstract BaseSpecification extends IntegrationSpec { public baseTest() { // setUp: // ... when: //

我的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:没有方法的签名:BaseSpecification.baseTest()适用于参数类型:()值:[] 可能的解决方案:any()、old(java.lang.Object)、any(groovy.lang.Closure)、notify()、wait()、Spy() 在


    你知道如何在spock集成测试中实现继承吗?

    好的,现在我明白你的意思了

    您几乎可以这样使用:

    class BaseSpecification extends IntegrationSpec {
    
        //User userInstance
    
        def setup() {
            // do your common stuff here like initialize a common user which is used everywhere
        }
    
        def cleanup() {
        }
    }
    
    class SpecificTestSpecification extends BaseSpecification {
    
        def setup() {
            // specific setup here. Will call the super setup automatically
        }
    
        def cleanup() {
        }
    
        void "test something now"() {
            // You can use that userInstance from super class here if defined.
        }
    }
    

    我不知道斯波克是否能做到。当我尝试时,我找不到重用spock语句的方法,我所做的就是编写一个
    BaseSpecification
    类,其中包含可以在spock语句中使用的实用方法

    这是一个示例测试

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

    这是一个单元测试,但我认为它也应该用于集成测试。

    您需要在SpecificClass类声明中扩展基类而不是IntegrationSpec。@ShashankAgrawal很抱歉我这么做了。我错了。好吧,那么你的代码本身的问题描述有错?(例外情况消失了吗?@ShashankAgrawal谢谢。我想这是你能得到的最接近的了。将留下问题,以防有人能提供更好的答案。问题是继承实际的测试。但是当你想到你可能无论如何都不应该这样做。
    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
        }
    }