Matching invocations of mock gives NPE

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Matching invocations of mock gives NPE

Kenneth Hansen
hello,
I have a problem when matching an invocation. It yields an NPE if I do not make the invocation matching itself return a value. Any idea why?

This test fails with a NPE if I do not use a '>> Observable.empty()' on the second matching in the then-block:
def 'request context with null body'() {
 given
:
 
RequestContext requestContext = generateRequestContext(null)

 
when:
 notifyHandler
.postNotification(requestContext).toBlocking().subscribe()

 
then:
 
1 * requestContext.getResponse().setStatus(HttpResponseStatus.BAD_REQUEST)
 
1 * requestContext.getResponse().writeStringAndFlush('Content is empty') >> Observable.empty() // NPE if >> Observable.empty() is omitted
 
1 * requestContext.getResponse().close()
}


This tests, however, is able to match without returning something at the same time:

def 'exception when inserting data'() {
 given
:
 
RequestContext requestContext = generateRequestContext()
 queueService
.insert(_, _) >> { throw new QueueServiceException("Error inserting data in queue") }

 
when:
 notifyHandler
.postNotification(requestContext).toBlocking().subscribe()

 
then:
 
1 * requestContext.getResponse().setStatus(HttpResponseStatus.BAD_REQUEST)
 
1 * requestContext.getResponse().writeStringAndFlush("""{"error":"Unable to notify to user 1","code":10003}""")
 
1 * requestContext.getResponse().close()
}



And the mocking of the RequestContext, for reference
private RequestContext generateRequestContext(String content = body) {
 
HttpServerRequest<ByteBuf> request = Mock()
 
HttpRequestHeaders requestHeaders = Mock()
 request
.getUri() >> '/subscribers/1/notification'
 request
.getHttpMethod() >> HttpMethod.POST
 
if (content != null && !content.isEmpty()) {
   request
.getContent() >> Observable.just(Unpooled.wrappedBuffer(content.getBytes(Charset.forName("UTF-8"))))
 
}
 request
.getHeaders() >> requestHeaders

 
HttpServerResponse<ByteBuf> response = Mock()
 
HttpResponseHeaders responseHeaders = Mock()
 response
.getHeaders() >> responseHeaders
 response
.writeStringAndFlush(_) >> Observable.empty()
 response
.close() >> Observable.empty()

 
new RequestContext(request, response)
}



--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.