How do I spin up a spek test using kotlin to test whether or not an HTTP method post has been called? Whats tripping me out is i'm having trouble mocking up the context. I'd like to pass in a method other then HttpMethod.POST to fire off the else block.
Currently fails with the message -
handlers.AHandlerTest > initializationError FAILED
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class ...handlers.AHandler
Mockito cannot mock/spy because :
- final class
at handlers.AHandlerTest$1.invoke(AHandlerTest.kt:76)
at handlers.AHandlerTest$1.invoke(AHandlerTest.kt:17)
It also fails saying context.request can't be null
import ratpack.handling.Context
class AHandler : Handler {
override fun handle(contex: Context) {
when (contex.request.method) {
HttpMethod.POST -> create(contex)
else -> {
contex.response.status(405)
contex.render("Unsupported method. Supported methods are: POST")
}
}
}
}
test file:
package handlers
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import handlers.AHandler
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import ratpack.handling.Context
import org.mockito.Mockito
import ratpack.http.HttpMethod
import kotlin.test.assertEquals
import kotlin.test.assertFails
class AHandlerTest: Spek({
val context = mock<Context>()
val httpReq = mock<Context> {
on { context.request.method }.doReturn(HttpMethod.DELETE)
}
describe("testing handler function") {
it("it should fail when not given a post method") {
}
}
})
here's what helped me get it to pass -
it passed the test after this