-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document test exception handling in the User Guide
This commit documents how exceptions in tests are handled by explaining the following. 1. Uncaught exceptions cause test failures by default 2. 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 This commit also introduces dedicated examples for assertThrows(), assertThrowsExactly(), and assertDoesNotThrow(). Closes #3780
- Loading branch information
1 parent
1e5de9e
commit f915d67
Showing
5 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
documentation/src/test/java/example/exception/AssertDoesNotThrowExceptionDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.exception; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
// @formatter:off | ||
// tag::user_guide[] | ||
|
||
class AssertDoesNotThrowExceptionDemo { | ||
|
||
@Test | ||
void assertDoesNotThrowExceptionTest() { | ||
assertDoesNotThrow(() -> { | ||
shouldNotThrowException(); | ||
}); | ||
} | ||
|
||
void shouldNotThrowException(){} | ||
} | ||
|
||
// end::user_guide[] | ||
// @formatter:on |
43 changes: 43 additions & 0 deletions
43
documentation/src/test/java/example/exception/ExceptionAssertionDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.exception; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
// @formatter:off | ||
// tag::user_guide[] | ||
|
||
class ExceptionAssertionDemo { | ||
|
||
@Test | ||
void testExpectedExceptionIsThrown() { | ||
// The following assertion succeeds because the code under assertion throws | ||
// the expected IllegalArgumentException | ||
// The assertion also returns the thrown exception which can be used for | ||
// further assertions like asserting the exception messages | ||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { | ||
throw new IllegalArgumentException("Expected exception"); | ||
}); | ||
assertEquals("Expected exception", exception.getMessage()); | ||
|
||
// The following assertion should also succeed because the code under assertion throws | ||
// IllegalArgumentException which is subclass of RuntimeException | ||
assertThrows(RuntimeException.class, () -> { | ||
throw new IllegalArgumentException("Expected exception"); | ||
}); | ||
} | ||
} | ||
|
||
// end::user_guide[] | ||
// @formatter:on |
46 changes: 46 additions & 0 deletions
46
documentation/src/test/java/example/exception/ExceptionAssertionExactDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.exception; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrowsExactly; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
// @formatter:off | ||
// tag::user_guide[] | ||
|
||
public class ExceptionAssertionExactDemo { | ||
|
||
// end::user_guide[] | ||
@extensions.ExpectToFail | ||
// tag::user_guide[] | ||
@Test | ||
void testExpectedExceptionIsThrown() { | ||
// The following assertion succeeds because the code under assertion throws | ||
// IllegalArgumentException which is exactly equal to the expected type | ||
// The assertion also returns the thrown exception which can be used for | ||
// further assertions like asserting the exception messages | ||
IllegalArgumentException exception = assertThrowsExactly(IllegalArgumentException.class, () -> { | ||
throw new IllegalArgumentException("Expected exception"); | ||
}); | ||
assertEquals("Expected exception", exception.getMessage()); | ||
|
||
// The following assertion should fail because the assertion expected exactly | ||
// RuntimeException to be thrown, not subclasses | ||
assertThrowsExactly(RuntimeException.class, () -> { | ||
throw new IllegalArgumentException("Expected exception"); | ||
}); | ||
} | ||
} | ||
|
||
// end::user_guide[] | ||
// @formatter:on |
31 changes: 31 additions & 0 deletions
31
documentation/src/test/java/example/exception/UncaughtExceptionHandlingDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.exception; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
// @formatter:off | ||
// tag::user_guide[] | ||
|
||
class UncaughtExceptionHandlingDemo { | ||
|
||
// end::user_guide[] | ||
@extensions.ExpectToFail | ||
// tag::user_guide[] | ||
@Test | ||
void failsDueToUncaughtException() { | ||
// the following thrown exception will cause a test failure | ||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
// end::user_guide[] | ||
// @formatter:on |