Mark
Mark

Reputation: 5622

Can I add a delay to a call to a Stub method in Spock?

I'm using the Spock Framework to test some Java classes. One thing I need to do is add a delay to a Stub method that I'm calling, in order to simulate a long-running method. Is this possible?

This looks possible using Mockito: Can I delay a stubbed method response with Mockito?. Is it possible using Spock?

Upvotes: 0

Views: 1046

Answers (2)

kriegaex
kriegaex

Reputation: 67407

Spock is a Groovy tool. Therefore, you have some syntactic sugar and do not need a tedious try-catch around Thread.sleep. You simply write:

// Sleep for 2.5 seconds
sleep 2500

Your test could look like this:

class Calculator {
  int multiply(int a, int b) {
    return a * b
  }
}
class MyClass {
  private final Calculator calculator

  MyClass(Calculator calculator) {
    this.calculator = calculator
  }

  int calculate() {
    return calculator.multiply(3, 4)
  }
}
import spock.lang.Specification

import static java.lang.System.currentTimeMillis

class WaitingTest extends Specification {
  static final int SLEEP_MILLIS = 250

  def "verify slow multiplication"() {
    given:
    Calculator calculator = Stub() {
      multiply(_, _) >> {
        sleep SLEEP_MILLIS
        42
      }
    }
    def myClass = new MyClass(calculator)
    def startTime = currentTimeMillis()

    expect:
    myClass.calculate() == 42
    currentTimeMillis() - startTime > SLEEP_MILLIS
  }
}

Try it in the Groovy web console.

Upvotes: 2

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27245

One thing I need to do is add a delay to a Stub method that I'm calling, in order to simulate a long-running method. Is this possible?

It is difficult to say for sure if this is the right thing to do without knowing more about the situation under test but if you are executing a method and want that to result in tying up the current thread for a period to simulate doing work, your mock method could invoke Thread.sleep.

Upvotes: 0

Related Questions