[Kotlin] thenThrow(Throwable) should be able to stub the method with any checked exceptions
See original GitHub issueHi,
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:
- Created 6 years ago
- Reactions:4
- Comments:12 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is a workaround :
also works if you don’t like Yoda mocks