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.

Include actual exception when `shouldThrowTheException` fails

See original GitHub issue

Actual situation

When a should throw the exception fails, the message states:

Expected foo.bar.TheExpectedException to be thrown expected:<class [foo.bar.TheExpected]Exception> but was:<class [kotlin.KotlinNullPointer]Exception>

Then I can see the stack trace of that ComparisonFailure (at least, in the IDE).

Problem

That indicates only the point of failure in the test (the assertion itself), but does not give me any information about the exception that did occur in reality (that was not expected). I need then to trace and/or modify temporarily my test code so it just fails (remove the assertion) and then I can see what happened.

Proposition

Include the actual exception as a cause of the thrown ComparisonFailure. That way, we can look at the stack trace to know exactly what happened instead of our expected exception.

In Exceptions.kt: Before:

        else throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}")

After:

        else throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}", e)

That logic should/could be applied to every assertions on exceptions, including the withMessage and withCause.

After effect

From the IDE, we can then click on the stack trace of the exception that caused the assertion to fail to be brought to the line that caused it in the code AND know exactly what happened from its message.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Adeynackcommented, Mar 3, 2020

I will de-a assign myself from it. I haven’t used Kotlin in almost 2 years. My focus is on completely different things now. My apologies for never completing this change.

1reaction
MarkusAmshovecommented, Jun 21, 2017

This looks like a really nice addition.

ComparisonFailure is a type from junit, so we wouldn’t be able to add a constructor. But maybe subtyping it works.

I initially chose to use the junit exceptions because the IDEs I use (IntelliJ and Eclipse) recognize them and can show a specific window (like a diff window for assertEquals failures).

However, the diff window wouldn’t be very useful here, as it just shows the two types.

I`d appreciate a pull request, because I don’t have a lot time in June at the moment, but would gladly review, merge and release it 😃 Otherwise I can look into it in July

Read more comments on GitHub >

github_iconTop Results From Across the Web

When to catch the Exception vs When to throw the Exceptions?
The guidelines in this second scenario are: Handle the generic Exception class; Add more information from current execution context; Log the error and...
Read more >
Everything you wanted to know about exceptions - PowerShell
A thrown exception is either be caught or it terminates the current execution. By default, a non-terminating error is generated by Write-Error ......
Read more >
Asserting exceptions in JUnit - cassiomolin
This post explores some techniques for asserting exceptions in Java with JUnit. Table of contents. Using try - catch with fail(); Using @Test ......
Read more >
How to Throw Exceptions in Python - Rollbar
As a Python developer, you need to think about possible exception situations and include error handling in your code.
Read more >
8. Errors and Exceptions — Python 3.11.1 documentation
Until now error messages haven't been more than mentioned, but if you have tried out the examples you have probably seen some.
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