Search code examples
testingjunitspockequalsverifier

How to use EqualsVerifier in a Spock test


I have been writing my tests using spock. But to test Equals Hashcode contracts, I am trying to use EqualsVerifier. So my test code looks like:

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

But this does not look like its running with spock.

How can I workaround this? I wish to prefer using spock for my tests.


Solution

  • It all works correctly but in spock it's a bit different, see:

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

    This spec fails since the exception is thrown - SomeClass needs to be corrected. Have a look at the great docs.