Search code examples
unit-testinggrailsspock

Grails Spock Unit Test - What is "1 *" and why "too few invocations"?


I see the following code within a Unit Test for a controller that I have inherited from others:

  when: "When the controller executes a registration"
      controller.index()

    then: "the signup should show registration again"
      1 * controller.cService.getRegions() >> [] 
      1 * controller.dService.chkAvail(_) >> "AVAILABLE"
      1 * controller.uService.createUser(_) >> { a-> throw new RuntimeException("Roll me back!")}
      1 * controller.pService.registerPayMethod(_) >> { cc-> true }
      view == "/signUp/su"

I understand the basics of spock unit tests, but I don't understand these 1 * lines.

I am also getting multiple errors such as:

junit.framework.AssertionFailedError: Too few invocations for:

1 * controller.cService.getRegions() >> []   (0 invocations)

Unmatched invocations (ordered by similarity):

None

Solution

  • You are telling Spock, that the method in question has to be invoked exactly once (1 * controller.cService.getRegions() >> [] means, getRegions of this service has to be called once (1 *) and will return an empty list (>> [])). But it hasn't. This is what the error message is telling you (0 invocations).