I want to write a unit test for my controller which uses multiple services inside it. How can I use multiple mockFor() for services? Tnx.
Asked
Active
Viewed 62 times
0
-
Are you specifically asking about how to use multiple `mockFor`, or asking more generally how to create multiple mocks? – Jeff Scott Brown Jun 15 '21 at 14:27
-
@JeffScottBrown Thanks for replying, my question is how to use multiple mockFor() when my controller uses multiple services inside it. – pioneer Jun 16 '21 at 04:33
-
"my question is how to use multiple mockFor() when my controller uses multiple services inside it." - The accepted answer doesn't use any `mockFor`. – Jeff Scott Brown Jun 16 '21 at 11:25
1 Answers
0
For example using spock for testing:
class ExampleControllerUnitSpec extends Specification {
def anyService
def anotherService
def setup() {
anyService = Mock(AnyService)
controller.anyService = anyService
anotherService = Mock(AnotherService)
controller.anotherService = anotherService
}
def "test"(){
when:
controller.action()
then:
1 * anyService.doSomething() >> "result"
3 * anotherService.doSomethingElse() >> "result2"
}
}

Michal_Szulc
- 4,097
- 6
- 32
- 59