question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Kotlin] thenThrow(Throwable) should be able to stub the method with any checked exceptions

See original GitHub issue

Hi,

Kotlin does not have checked exceptions. https://kotlinlang.org/docs/reference/exceptions.html#checked-exceptions

However, ThrowsException#validateFor(InvocationOnMock) does not allow the checked exception that does not match the method signature. Therefore, the following test fails.

class MyTest {

    @Test(expected = MyException::class)
    fun test() {
        val mock = Mockito.mock(I::class.java)
        Mockito.`when`(mock.doIt()).thenThrow(MyException()) // This line throws a MockitoException
        mock.doIt()
    }

    interface I {
        fun doIt(): String
    }

    class MyException : Exception()

}

I’m going to file the PR to fix this.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

7reactions
jibiduscommented, Jun 21, 2018

Here is a workaround :

doAnswer { throw MyException() }
            .`when`(mock)
            .doIt()
5reactions
drawerscommented, Mar 2, 2021
`when`(mock.doIt()).thenAnswer { throw MyException() }

also works if you don’t like Yoda mocks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Throwing Exceptions with Mockito in Kotlin - Stack Overflow
Kotlin does not support creating methods that throw checked exceptions. You can either define makeRequest in Java, ...
Read more >
Mocking Exception Throwing using Mockito - Baeldung
In this quick tutorial, we'll focus on how to configure a method call to throw an exception with Mockito.
Read more >
Those sneaky Kotlin Exceptions - Medium
So the Kotlin compiler hides the exception behind Throwable , which is the interface that both, checked and unchecked exceptions implement.
Read more >
Exceptions - Kotlin Programming Language
All exception classes in Kotlin inherit the Throwable class. Every exception has a message, a stack trace, and an optional cause.
Read more >
`when` and `do*` | Migrating from Mockito | MockK Guidebook
thenThrow / doThrow #. Rather than returning values, stubs can throw errors. Mockito lets exceptions be specified with one of two approaches: val ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found