how to call a method or closure in a where clause in a spock test
Asked Answered
C

2

8

I'm working with Spock tests atm and I wonder if anything like this is even possbile. My approaches don't work and I wonder if anyone of you had similiar intentions and found a way.

I want to call a method or a closure which must only be called for each respective where-clause in order to setup some things. I can not just call all of these methods as it would ruin my test. The only way I found so far is to check what the current state is and call the method accordingly in an if statement like: if(state==SomeStateEnum.FIRST_STATE){somePrivateMethodFromSpec()} but I wonder if it couldn't be done in a better way. I hope my intentions are clear (sorry, I'm no native speaker) Below is some example code which may be a bit better to understand of what I want to do. Thank you in advance.

def 'is this even possible?'() {
    when:
    def resultState = service.someServiceMethod(param)

    then:
    resultState == state

    where:
    state                       | param     | method
    SomeStateEnum.FIRST_STATE   | 'param1'  | somePrivateMethodFromSpec()
    SomeStateEnum.SECOND_STATE  | 'param2'  | someOtherPrivateMethodFromSpec()
}

private def somePrivateMethodFromSpec() {
    someServiceMock.demand.AAA() {}
}

private def someOtherPrivateMethodFromSpec() {
    someServiceMock.demand.BBB() {}
}

def 'or maybe something like this?'() {
    when:
    closure.call()
    def resultState = service.someServiceMethod(param)

    then:
    resultState == state

    where:
    state                       | param     | closure
    SomeStateEnum.FIRST_STATE   | 'param1'  | {println '1'}
    SomeStateEnum.SECOND_STATE  | 'param2'  | {println '2'}
}

The solution is:

def 'this will work'() {
    "$someOtherPrivateMethodFromSpec"()
    "$somePrivateMethodFromSpec"()
    def resultState = service.someServiceMethod(param)

    then:
    resultState == state

    where:
    state                       | param     | method
    SomeStateEnum.FIRST_STATE   | 'param1'  | "somePrivateMethodFromSpec"
    SomeStateEnum.SECOND_STATE  | 'param2'  | "someOtherPrivateMethodFromSpec"
}

private def somePrivateMethodFromSpec() {
    someServiceMock.demand.AAA() {}
}

private def someOtherPrivateMethodFromSpec() {
    someServiceMock.demand.BBB() {}
}
Cervix answered 14/1, 2014 at 12:39 Comment(0)
C
7

Not sure if this is exactly what you need but you can try it:

class MyFirstSpec extends Specification {
  def "let's try this!"() {
    expect:
      "${method}"() == method
    where:
      method << ["method1", "method2"]
  }

  private String method1(){
     return "method1"
  }

  private String method2(){
     return "method2"
  }
}

Charpentier answered 14/1, 2014 at 13:33 Comment(1)
Yes, thats what I needed. Thank you.:) I editet my post with the solution according to my problem.Cervix
D
12

In order to call the closure way you just need to add more parenthesis around the closure in the where clause.

E.g. ({ println("1") })

Or as the full spec.

def 'or maybe something like this?'() {
    when:
    closure.call()
    def resultState = service.someServiceMethod(param)

    then:
    resultState == state

    where:
    state                       | param     | closure
    SomeStateEnum.FIRST_STATE   | 'param1'  | ({println '1'}) 
    SomeStateEnum.SECOND_STATE  | 'param2'  | ({println '2'})
}
Dhoti answered 4/4, 2016 at 22:42 Comment(0)
C
7

Not sure if this is exactly what you need but you can try it:

class MyFirstSpec extends Specification {
  def "let's try this!"() {
    expect:
      "${method}"() == method
    where:
      method << ["method1", "method2"]
  }

  private String method1(){
     return "method1"
  }

  private String method2(){
     return "method2"
  }
}

Charpentier answered 14/1, 2014 at 13:33 Comment(1)
Yes, thats what I needed. Thank you.:) I editet my post with the solution according to my problem.Cervix

© 2022 - 2024 — McMap. All rights reserved.