2

Spock does not detect doTip method invocation

(I need shared for some "where" blocks.)

Used latest groovy and spock.

Why this code is wrong?

How fix it?

import spock.lang.Shared import spock.lang.Specification class Test extends Specification { def controller @Shared String g = "" @Shared def tip = Mock(Tip) def "test"() { controller = new TController(tip: tip) when: controller.transform(g) then: 1 * tip.doTip(_) } } class Tip { def doTip(String f) {} } class TController { Tip tip def transform(String g) { tip.doTip(g) } } 

1 Answer 1

2

Use setup() to create the mock as shown below:

@Grab(group='org.spockframework', module='spock-core', version='1.0-groovy-2.4') import spock.lang.* class Test extends Specification { def controller @Shared String g = "" @Shared tip def setup() { tip = Mock(Tip) } def "test"() { given: controller = new TController(tip: tip) when: controller.transform(g) then: 1 * tip.doTip(_) } } class Tip { def doTip(String f) {} } class TController { Tip tip def transform(String g) { tip.doTip(g) } } 

Result

JUnit 4 Runner, Tests: 1, Failures: 0, Time: 78 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.