Cannot throw exception
See original GitHub issueI have a method that throws a custom exception
class SiteRuleService {
fun extractDomain(uri: URI): String {
throw InvalidUrlException("Invalid URL or domain <${uri.toASCIIString()}>")
}
}
class InvalidUrlException(override val message: String = "") : Exception(message)
And a test to exercise this method:
@Test
fun testFindPagechat_InvalidUrlForBadUrl() {
val inputUri = URI("http://example.com")
doThrow(InvalidUrlException("Invalid URL or domain"))
.whenever(siteRuleService).extractDomain(inputUri)
try {
siteRuleService.extractDomain(inputUri)
fail("Expected exception")
} catch (e: InvalidUrlException) {
assertEquals("Invalid URL or domain", e.message)
}
}
But Mockito complains because the method doesnt’ declare IUE
as a checked exception (since we can’t in Kotlin):
org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: net.forumforall.kuorum.pagechat.InvalidUrlException: Invalid URL or domain
Any way around this?
Issue Analytics
- State:
- Created 6 years ago
- Comments:9
Top Results From Across the Web
Why Can't I Throw Exception In A Method(Java)
An ArithmeticException is a RuntimeException , so it doesn't need to be declared in a throws clause or caught by a catch block....
Read more >How to Throw Exceptions in Java
Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception...
Read more >When you can't throw an exception
One problem with checked exceptions is that sometimes you simply aren't allowed to throw them. In particular, if you are overriding a method ......
Read more >How to Throw Exceptions (The Java™ Tutorials > Essential ...
Most programs throw and catch objects that derive from the Exception class. An Exception indicates that a problem occurred, but it is not...
Read more >Types of Exceptions in Java
Learn about the different types of exceptions in Java (checked and unchecked) and see specific examples.
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 :
Is it a good idea to add annotations to source code just for testing purposes? I understand writing testable code and writing in a way that makes it testable, but this seems like a stretch and introduces something excluded from the language