spockframework/spock

GroovyMock object on a final class doesn't work properly inside statically compiled service

Open

#765 geöffnet am 7. Sept. 2017

Auf GitHub ansehen
 (4 Kommentare) (7 Reaktionen) (0 zugewiesene Personen)Java (466 Forks)batch import
help wanted

Repository-Metriken

Stars
 (3.457 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 2T 8h) (11 gemergte PRs in 30 T)

Beschreibung

Issue description

GroovyMock interactions for final class doesn't work inside@CompileStatic annotated class or in plain java class. In this case mock object will use real method implementation instead of provided interaction.

Minimal code to reproduce

import groovy.transform.CompileStatic
import spock.lang.Specification

class UserServiceSpec extends Specification {
    def "test final class mock interactions"() {
        setup:
            def mock = GroovyMock(FinalClass) {
                getName() >> "Foo Bar"
            }
        when:
            new StaticService().getName(mock)
        then:
            thrown(UnsupportedOperationException)
        expect:
            new GroovyService().getName(mock) == "FOO BAR"
    }
}

final class FinalClass {
    String getName() {
        throw new UnsupportedOperationException("Not implemented")    
    }
}

class GroovyService {
    String getName(FinalClass obj) {
        obj.name.toUpperCase()
    }
}

@CompileStatic
class StaticService {
    String getName(FinalClass obj) {
        obj.name.toUpperCase()
    }
}

Environment

Java: OpenJDK Runtime Environment (build 1.8.0_144-b01) Gradle: 3.5

Gradle dependencies

compile 'org.codehaus.groovy:groovy-all:2.4.12'
compile 'org.spockframework:spock-core:1.1-groovy-2.4'
compile 'cglib:cglib-nodep:3.2.5'

Contributor Guide