How Spock Deals with Equality of Mocked Objects

I am testing the behavior of a class using the SortedSet of Library objects (a regular class is not an interface, so I brought cglib-nodep). I need to check the behavior of a class when a sorted set has multiple objects. Library objects were mocked this way:

Library library = Mock()
Library library2 = Mock()

Then I create a TreeSet:

def libraries = [library, library2] as TreeSet

and call the system under the test method:

sut.doStuff(libraries).

When I debug this test, I see that the libraries are a SortedSet with one element. This, apparently, is a consequence of how Spock deals with equality:

def "equality test"() {
    expect:
        library == library2
}

passes when I run the test. Is there a way I can get around this behavior?

EDIT: Changed = to == because I cannot print

+4
2

. (groovy console script):

@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')

import spock.lang.*

class Test extends Specification {
    def "not comparable mocks are not equal"() {
        given:
        def a1 = Mock(A)
        def a2 = Mock(A)

        expect:
        a1.hashCode() != a2.hashCode()
        !a1.equals(a2)
        !(a1 == a2)        
    }

    def "comparable mocks are not equal"() {
        given:
        def a1 = Mock(AC)
        def a2 = Mock(AC)

        expect:
        a1.hashCode() != a2.hashCode()
        !a1.equals(a2)
        !(a1 == a2)        
    }

    def "cannot create TreeSet when POJOs are not comparable"() {
        given:
        def a1 = Mock(A)
        def a2 = Mock(A)

        and:    
        a1.hashCode() != a2.hashCode()
        !a1.equals(a2)
        !(a1 == a2)     

        when:
        new TreeSet([a1,a2])

        then:
        def e = thrown(ClassCastException)
        e.message.endsWith('cannot be cast to java.lang.Comparable')
    } 

    def "there a problem with Comparable Mocks"() {
         given:
        def a1 = Mock(AC)
        def a2 = Mock(AC)

        and:    
        a1.hashCode() != a2.hashCode()
        !a1.equals(a2)
        !(a1 == a2)     

        when:
        def s = new TreeSet([a1,a2])

        then:
        s.size() == 2
    }

    def "with HashSet it works as expected"() {
        given:
        def a1 = Mock(AC) 
        def a2 = Mock(AC) 

        and:    
        a1.hashCode() != a2.hashCode()
        !a1.equals(a2)
        !(a1 == a2)     

        when:
        def s = new HashSet([a1,a2])

        then:
        s.size() == 2
    }
}

class A {}

class AC implements Comparable {

    int compareTo(Object o) {
        1 //whatever value may be here, it not called
    }
}

, Comparable.

  • , - . , .
  • , , Comparable. . , .
  • , TreeSet POJO, . .
  • , . , a TreeSet, Comparable mocks, 2. , 1.
  • , 4 HashSet.

IMO , spock. Spock cglib mockin, .

, compareTo() :

@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')

import spock.lang.*

class Test extends Specification {

    def "there a problem with Comparable Mocks"() {
         given:
        def a1 = Mock(AC) {
            compareTo(_) >> 3145
        }
        def a2 = Mock(AC) {
            compareTo(_) >> 3146
        }

        and:    
        a1.hashCode() != a2.hashCode()
        !a1.equals(a2)
        !(a1 == a2)     

        when:
        def s = new TreeSet([a1,a2])

        then:
        s.size() == 2
    }
}

class AC implements Comparable {

    int compareTo(Object o) {
        1 //whatever value may be here, it not called
    }
}
+3

equals() , Mock() equals(). Groovy == equals(). Comparable s c1.compareTo(c2) == 0. , a TreeSet compareTo() equals . :

  • mocks compareTo() .
  • TreeSet.
  • , compareTo , mocks.

PS: , Mock(Comparable) / Stub(Comparable) compareTo , equals(). ?

+2

All Articles