GroovyMock over same constructor signatures broken #653

claudiocarcaci opened this Issue Sep 26, 2016 · 0 comments


None yet

1 participant


Hi all, I'm reporting an issue on GroovyMock over constructors.
My Groovy version is 2.4.0 packed within Grails 3.2.0.M2.

I have three classes:

class Foo {
    String foo

    public Foo(String foo) { = foo

class Bar {
    String bar

    public Bar(String bar) { = bar

class FooBar {
    private Foo foo
    private Bar bar

    public FooBar() { = new Foo("foo") = new Bar("bar")

    public String printMe() {
        return "${foo.getFoo()} ${bar.getBar()}"

I want to Test the FooBar class by catching constructors of dependencies classes Foo and Bar.

import com.scratch.Bar
import com.scratch.Foo
import com.scratch.FooBar
import spock.lang.Specification

import java.util.logging.Logger

public class Mockba extends Specification {
    Logger log = Logger.getLogger("")

    def "foo"() {

        GroovyMock(Foo, global: true)
        GroovyMock(Bar, global: true)
        Foo foo = GroovyMock(Foo)
        Bar bar = GroovyMock(Bar)


        FooBar fooBar = new FooBar()"${fooBar.printMe()}")

        new Foo((String)_) >> foo
        new Bar((String)_) >> bar
        foo.getFoo() >> "oof"
        bar.getBar() >> "rab"
        assert 1 == 1 // That's fake

The GroovyMock should catch the constructor call over Foo and Bar and would supply the mocked version of these objects.

But unfortunately it seems that there is a weird thing that happen because when the instruction: = new Bar("bar")

inside the FooBar() constructor is called. The GroovyMock returns the Foo mocked object. The debug watch over new Bar("") gives:

"Mock for type 'Foo' named 'foo'" as Mock result
That's a nonsense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment