【问题标题】:How to use EqualsVerifier in a Spock test如何在 Spock 测试中使用 EqualsVerifier
【发布时间】:2015-08-21 18:18:48
【问题描述】:

我一直在使用 spock 编写测试。但是为了测试 Equals Hashcode 合约,我尝试使用EqualsVerifier。所以我的测试代码如下:

def "test equals hashcode contract"() {
    EqualsVerifier.forClass(Content.class).verify();
}

但这看起来不像是用 spock 运行的。

我该如何解决这个问题?我希望更喜欢使用 spock 进行测试。

【问题讨论】:

    标签: testing junit spock equalsverifier


    【解决方案1】:

    一切正常,但在 spock 中有点不同,请参阅:

    @Grab('org.spockframework:spock-core:0.7-groovy-2.0')
    @Grab('cglib:cglib-nodep:3.1')
    @Grab('nl.jqno.equalsverifier:equalsverifier:1.7.2')
    
    import spock.lang.*
    import nl.jqno.equalsverifier.*
    
    class Test extends Specification {
        def 'sample'() {
            when:
            EqualsVerifier.forClass(SomeClass).verify()
    
            then:
            noExceptionThrown()
        }
    }
    
    class SomeClass {}
    

    规范 失败,因为抛出异常 is - SomeClass 需要更正。看看伟大的docs

    【讨论】:

    • 我不需要 Grabs - 你知道为什么吗?仅供参考:我正在使用 maven 构建我的项目。
    • 可能是因为你使用 maven 或 gradle 并且依赖项是在其他地方导入的?我提供的脚本是可自行运行的。你可以把它保存到whatever.groovy 然后运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2020-07-22
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多