3

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.

Opal
  • 81,889
  • 28
  • 189
  • 210
Neel
  • 2,100
  • 5
  • 24
  • 47

1 Answers1

4

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.

Opal
  • 81,889
  • 28
  • 189
  • 210
  • I did not need the Grabs - do you know why? FYI: I am building my project using maven. – Neel Jun 07 '15 at 16:57
  • 2
    Probably because you use maven or gradle and the dependencies are imported elsewhere? The script I provided is self runnable. You can save it to `whatever.groovy` and just run. – Opal Jun 07 '15 at 16:58