0

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?

Mark
  • 4,970
  • 5
  • 42
  • 66

2 Answers2

2

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
0

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.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47