Hi, I have a controller called ApiController that uses the ApiService service like this:
def createCategory(){ def jsonObj = request.JSON jsonObj.each{ key, value -> params.put(key,value) } render apiService.createCategory(params) }
Which works great. But I can not write a test for this.
Here is how far I got:
@TestFor(ApiController) @Mock([Category,ApiService]) class CategorySpec extends Specification { def setup() { } def cleanup() { } void "test"() { setup: def apiService = Mock(ApiService) when: request.method = 'POST' request.requestMethod = 'POST' params.categoryID = 'test' controller.createCategory() then: println(response) 1==1 }
From this I get the following error:
java.lang.NullPointerException: Cannot invoke method createCategory() on null object
This is obviously because it cannot see my apiService bean. So my question is: how to do this in Spock?
source share