I will describe a weird (maybe wrong) behavior.
I have three classes:
class Foo {
String foo
public Foo(String foo) {
this.foo = foo
}
}
class Bar {
String bar
public Bar(String bar) {
this.bar = bar
}
}
class FooBar {
private Foo foo
private Bar bar
public FooBar() {
this.foo = new Foo("foo")
this.bar = 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"() {
given:
GroovyMock(Foo, global: true)
GroovyMock(Bar, global: true)
Foo foo = GroovyMock(Foo)
Bar bar = GroovyMock(Bar)
when:
FooBar fooBar = new FooBar()
log.info("${fooBar.printMe()}")
then:
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 supply the mocked version of these objects.
But unfortunately it seems that there is a weird thing that happen because when the instruction:
this.bar = 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 for me.
Any suggestion?
Aucun commentaire:
Enregistrer un commentaire