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.

Cannot throw exception

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:9

github_iconTop GitHub Comments

50reactions
jibiduscommented, Jun 21, 2018

Here is a workaround :

doAnswer { throw InvalidUrlException("Invalid URL or domain") }
            .whenever(siteRuleService)
            .extractDomain(inputUri)
11reactions
james-ralstoncommented, Dec 1, 2017

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

Read more comments on GitHub >

github_iconTop 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 >

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