I work with Spock certificates and I wonder if something like this is possible. My approaches do not work, and I wonder if any of you had similar intentions and found a way.
I want to call a method or closure that needs to be called only for each corresponding where-clause in order to configure some things. I can’t just call all of these methods, as this will ruin my test. The only way I've found so far is to check what the current state is and call the method appropriately in the if statement: if (state == SomeStateEnum.FIRST_STATE) {somePrivateMethodFromSpec ()}, but I'm wondering if it failed make at its best. I hope my intentions are clear (sorry, I'm not a native speaker). Below is an example of code that might be a little better to understand 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'}
}
Decision:
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() {}
}
source
share