title: Throwing Exceptions with Mockito in Kotlin
body: <p>I am using Kotlin and am trying to throw an Exception on a specific method invocation but always get the following error</p>
  
  <pre><code>Checked exception is invalid for this method!
  Invalid: exceptions.ServiceException
  </code></pre>
  
  <p>This is the test</p>
  
  <pre><code>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")
  }
  </code></pre>
  
  <p>Basically the <code>getUserDataByNameAndRegion</code> method will invoke the <code>makeRequest</code> method and with this test I want to validate that the method is handling the result of the stubbed method correctly.</p>
  
  <p>The original method looks like this </p>
  
  <pre><code>@Throws(NotFoundException::class, ServiceException::class)
  fun makeRequest(url: String, key : String) : String {
      val con = prepareConnection(url, key)
      val statusCode = con.responseCode
      when {
          (statusCode == 400) -&gt; throw ServiceException("Bad Request")
          (statusCode == 401) -&gt; throw ServiceException("Unauthorized")
          (statusCode == 403) -&gt; throw ServiceException("Forbidden")
          (statusCode == 404) -&gt; throw NotFoundException("Data not Found")
          (statusCode == 415) -&gt; throw ServiceException("Unsupported Media Type")
          (statusCode == 429) -&gt; throw ServiceException("Rate limit exceeded")
          (statusCode == 500) -&gt; throw ServiceException("Internal Server Error")
          (statusCode == 502) -&gt; throw ServiceException("Bad Gateway")
          (statusCode == 503) -&gt; throw ServiceException("Service unavailable")
          (statusCode == 504) -&gt; throw ServiceException("Gateway timeout")
          (statusCode == 200) -&gt; {
              return getStringResponseFromConnection(con)
          }
          else -&gt; {
              throw ServiceException("Respondend with Statuscode ${statusCode}")
          }
      }
  }
  </code></pre>