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

Document test exception handling #3730

Closed
1 task
Marcono1234 opened this issue Mar 15, 2024 · 7 comments
Closed
1 task

Document test exception handling #3730

Marcono1234 opened this issue Mar 15, 2024 · 7 comments

Comments

@Marcono1234
Copy link
Contributor

Marcono1234 commented Mar 15, 2024

It seems there is currently not one single place which describes how exceptions in tests are handled. This can lead to confusion regarding behavior for unexpected exceptions, especially when they are checked exceptions, see for example #3508.
This information is currently split across the documentation, such as the assertDoesNotThrow method documentation, which says:

Although any exception thrown from a test method will cause the test to fail

JUnit 4 has a separate FAQ entry for this1.

It might therefore be good to have one place (probably a new User Guide section?) where the exception handling is documented:

  • Uncaught exceptions cause a test error by default
    • for checked exceptions a throws clause has to be added to the test method
    • usage of throws clause has no effect (e.g. it does not indicate an "expected exception")
    • (maybe) recommend using throws Exception instead of throws CheckedExceptionA, CheckedExceptionB, CheckedExceptionC since the latter introduces a lot of unnecessary clutter2
    • can use assertDoesNotThrow to explicitly indicate unexpected exceptions, but it is not required
    • (maybe) mention special exception types which cause test to be skipped or aborted
  • Expected exceptions can be tested for using assertThrows / assertThrowsExactly
  • Difference between test failure (i.e. assertion failure) and test error (i.e. unexpected exception)3

But this section should probably be targeted at regular users, so it should not go into detail regarding how extensions can handle exceptions. That seems to already be covered in the User Guide section about extensions.

What do you think?

Deliverables

  • A section in the User Guide about test exception handling

Footnotes

  1. Though that entry is slightly confusing because it recommends using throws ... for IndexOutOfBoundsException, which is an unchecked exception.

  2. It seems not everyone agrees that using throws Exception there is fine. But personally I am not sure if developers really deliberately list all checked exceptions, or if most often they just use the IDE action for adding a throws clause.

  3. I am not sure though how strong this separation still exists for JUnit 5. I thought I head read about it in the JUnit 5 documentation before, but I cannot find it right now. And in some cases there seems to be no such differentiation, e.g. assertAll()'s exception-handling behavior turns all test programming errors into test failures #3436.

@marcphilipp
Copy link
Member

Team decision:

  • Document that uncaught exceptions cause test failures by default
  • Document that assertions are implemented using exceptions
  • Document that JUnit 5 does not differentiate between failed assertions and other exceptions but IDEs and other tools may choose to do so by checking for instances of AssertionError
  • Add assertDoesNotThrow example for Java

@driptaroop
Copy link
Contributor

If no-one has taken this up yet I would like to work on it if that's ok. Although since I have never contributed here a gentle guidance would be helpful.
As far as I have understood, the new documentation section for exceptions testing should be put in here right?

@marcphilipp
Copy link
Member

Yes, I think we should add an "Other Exceptions" section after "Assertions" and "Assumptions".

@driptaroop Feel free to take this on if you're still interested. Sorry for the delay!

@driptaroop
Copy link
Contributor

driptaroop commented Apr 15, 2024

@marcphilipp thanks. I will take it up and assign the issue to myself.

PS: couldn't figure out how to assign the issue to myself but I will take it up nonetheless :)

@marcphilipp marcphilipp added this to the 5.11 M2 milestone Apr 15, 2024
driptaroop added a commit to driptaroop/junit5 that referenced this issue Apr 16, 2024
Documents how exceptions in tests are handled. This PR contains documentation that describes,

1. Uncaught exceptions cause test failures by default
2. How assertions are implemented using exceptions
3. JUnit 5 does not differentiate between failed assertions and other exceptions but IDEs and other tools may choose to do so by checking for instances of AssertionError
4. Adds assertDoesNotThrow example for Java

Solves Issue: junit-team#3730

---

I hereby agree to the terms of the JUnit Contributor License Agreement.
@driptaroop

This comment was marked as outdated.

driptaroop pushed a commit to driptaroop/junit5 that referenced this issue Apr 17, 2024
As pointed out in the PR, this commit changes the title of the topic to 'Exception Handling'

Solves Issue: junit-team#3730

---

I hereby agree to the terms of the JUnit Contributor License Agreement.
driptaroop pushed a commit to driptaroop/junit5 that referenced this issue Apr 17, 2024
This PR fixes various comments,

1. added cross ref to "Third-party Assertion Libraries"
2. wrapped lines to 90 char
3. made the anchors hierarchical
4. added note for assertJ's no-exception assertion
5. used static imports for Assertions
6. created separate sections for assertThrows and assertThrowsExactly
7. explicitly mentioned that explicitly mention that a throws clause has no effect.

Solves Issue: junit-team#3730

---

I hereby agree to the terms of the JUnit Contributor License Agreement.
driptaroop pushed a commit to driptaroop/junit5 that referenced this issue Apr 18, 2024
fix lint issues

Solves Issue: junit-team#3730

---

I hereby agree to the terms of the JUnit Contributor License Agreement.
driptaroop pushed a commit to driptaroop/junit5 that referenced this issue Apr 18, 2024
Solves Issue: junit-team#3730

---

I hereby agree to the terms of the JUnit Contributor License Agreement.
driptaroop pushed a commit to driptaroop/junit5 that referenced this issue Apr 18, 2024
Solves Issue: junit-team#3730

---

I hereby agree to the terms of the JUnit Contributor License Agreement.
driptaroop pushed a commit to driptaroop/junit5 that referenced this issue Apr 18, 2024
Solves Issue: junit-team#3730

---

I hereby agree to the terms of the JUnit Contributor License Agreement.
driptaroop pushed a commit to driptaroop/junit5 that referenced this issue Apr 19, 2024
1. added documentation for assertThrows and assertThrowsExactly return
type and provided examples.

2. converted all the in code assertion messages to comments
3. fixed a mistake in test

Solves Issue: junit-team#3730

---

I hereby agree to the terms of the JUnit Contributor License Agreement.
driptaroop pushed a commit to driptaroop/junit5 that referenced this issue Apr 19, 2024
Solves Issue: junit-team#3730

---

I hereby agree to the terms of the JUnit Contributor License Agreement.
@sbrannen sbrannen self-assigned this Apr 20, 2024
@sbrannen sbrannen modified the milestones: 5.11 M2, 5.11 M1 Apr 20, 2024
@sbrannen
Copy link
Member

This has been addressed in f915d67 and 27f3b7c.

@sbrannen
Copy link
Member

The new "Exception Handling" section can now be viewed in the snapshot documentation:

https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-exceptions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants