14

I am using Kotlin and am trying to throw an Exception on a specific method invocation but always get the following error

Checked exception is invalid for this method!
Invalid: exceptions.ServiceException

This is the test

val client : IClient = Mockito.spy(Client(Cnf("https://:region.example.com", key)))


@Test(expected = ServiceException::class)
fun test400ResponseFrom() {
    val url = "https://example.com/example/user/v3/user/by-name/JACKAPPLE"

    Mockito.doThrow(ServiceException("Bad Request")).`when`(client).makeRequest(url ,riotkey)
    client.getUserDataByNameAndRegion("jackapple", "BR")
}

Basically the getUserDataByNameAndRegion method will invoke the makeRequest method and with this test I want to validate that the method is handling the result of the stubbed method correctly.

The original method looks like this

@Throws(NotFoundException::class, ServiceException::class)
fun makeRequest(url: String, key : String) : String {
    val con = prepareConnection(url, key)
    val statusCode = con.responseCode
    when {
        (statusCode == 400) -> throw ServiceException("Bad Request")
        (statusCode == 401) -> throw ServiceException("Unauthorized")
        (statusCode == 403) -> throw ServiceException("Forbidden")
        (statusCode == 404) -> throw NotFoundException("Data not Found")
        (statusCode == 415) -> throw ServiceException("Unsupported Media Type")
        (statusCode == 429) -> throw ServiceException("Rate limit exceeded")
        (statusCode == 500) -> throw ServiceException("Internal Server Error")
        (statusCode == 502) -> throw ServiceException("Bad Gateway")
        (statusCode == 503) -> throw ServiceException("Service unavailable")
        (statusCode == 504) -> throw ServiceException("Gateway timeout")
        (statusCode == 200) -> {
            return getStringResponseFromConnection(con)
        }
        else -> {
            throw ServiceException("Respondend with Statuscode ${statusCode}")
        }
    }
}
Jakob Abfalter
  • 4,270
  • 14
  • 48
  • 81

4 Answers4

7

You can try the following idea (taken from similar discussion on github):

.doAnswer { throw ServiceException("Bad Request") }
AbstractVoid
  • 2,399
  • 1
  • 33
  • 34
  • 1
    this works for me, the `answer` function seems to work. `given(yourClass.someMethod()).willAnswer { throw ServiceException() }` – Tenten Ponce Apr 23 '20 at 17:17
4

Kotlin does not support creating methods that throw checked exceptions. You can either define makeRequest in Java, or change ServiceException to extend RuntimeException.

Paul Hicks
  • 11,495
  • 3
  • 41
  • 67
2

You can use the following:

`when`(someClassMocked.muFunction).thenThrow(SocketException())
Tommy
  • 106
  • 1
  • 6
0

In my case I was using an underlying Java client that threw checked exceptions. My Kotlin code needed to handle one variation of these exceptions and so to test this code path I was able to use:

whenever(someObject.doesSomething(any()).then {
    throw MyCheckedException("Error")
}
fbailey
  • 241
  • 4
  • 16