Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include actual exception when shouldThrowTheException fails #45

Open
Adeynack opened this issue Jun 21, 2017 · 14 comments
Open

Include actual exception when shouldThrowTheException fails #45

Adeynack opened this issue Jun 21, 2017 · 14 comments
Labels
Projects

Comments

@Adeynack
Copy link

Adeynack commented Jun 21, 2017

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.

@Adeynack
Copy link
Author

It is something I could create a pull-request for, if you:

  1. feel that it is in interesting feature (it would definitely make MY life as a user of kluent easier!)
  2. it is something you would not work on anytime soon

@Adeynack
Copy link
Author

ComparisonException would need a constructor that accepts a cause.

@MarkusAmshove
Copy link
Owner

MarkusAmshove commented 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

@Adeynack
Copy link
Author

Thanks for the answer, @MarkusAmshove . I will get to it in the next days probably. Already copied a bit of your code in my project to move forward with it, so it is basically "already started" ;)

@Adeynack
Copy link
Author

@MarkusAmshove : How should we proceed for the contribution? I cannot find a CONTRIBUTING file to guide me.

  1. I am added to the project and create a pull-request directly on it
  2. I fork and then create a pull request from my fork to yours

Please advise!

@MarkusAmshove
Copy link
Owner

You're right, sorry. I'll put it on my TODO list.

In the past the contributers all forked this repository and opened a pull request from within the fork :-)

@Adeynack
Copy link
Author

I will do that then! Thanks for the response.

@Adeynack
Copy link
Author

@MarkusAmshove : You can assign this issue to me (I cannot perform that change), so no one starts work in parallel. My fork is not at git@github.com:Adeynack/Kluent.git. I will create a PR when ready.

@Adeynack
Copy link
Author

@MarkusAmshove : I just realised what you meant by IDE support earlier. I was wondering what you were talking about with the diff view and stuff like that. I just realised that I am using Kluent enclusively with Spek, and when one do so, those nice IDE features disappear. In my current setup (Spek/Kluent), I can only rely on the exception's message to understand what went wrong in my test. Not super nice. There is a LOT of work to do in the Spek plugin for IntelliJ.

@MarkusAmshove
Copy link
Owner

Yeah, I've wanted to port the unittests from Kluent to plain old JUnit, but we now have around 900 tests, so I just keep them that way :-)

We're using an old Spek version because it doesn't rely on the IntelliJ plugin (thinking about Eclipse users), maybe it would work as expected on the most recent version of Spek.
I'll look into updating it in the future.

@Adeynack
Copy link
Author

Did you mean "from Spek to JUnit"?

@MarkusAmshove
Copy link
Owner

Yeah, also because Gradle doesn't work well with the Spek version we use (it counts over a thousand tests, which aren't there). But I think this will be fixed when I upgrade Spek

@MarkusAmshove MarkusAmshove added this to To Do in Kluent 2.0 via automation Nov 21, 2018
@lucasqueiroz
Copy link

Any updates on this?

@Adeynack
Copy link
Author

Adeynack commented 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.

@Adeynack Adeynack removed their assignment Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Kluent 2.0
  
To Do
Development

No branches or pull requests

3 participants