-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Introduce extension APIs to handle exceptions from lifecycle callback methods #1868
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2f89413
Introduce extension APIs to handle exceptions from lifecycle methods
pietrygamat 280fb44
Implement tests for LifecycleMethodExecutionExceptionHandler
pietrygamat c239e4d
Add user guide and release notes entries on LifecycleMethodExecutionE…
pietrygamat ba99767
Apply suggestions from code review
pietrygamat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Binary file modified
BIN
-77.8 KB
(38%)
documentation/src/docs/asciidoc/user-guide/images/extensions_lifecycle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions
62
documentation/src/test/java/example/exception/MultipleHandlersTestCase.java
This file contains hidden or 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,62 @@ | ||
| /* | ||
| * Copyright 2015-2019 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 example.exception.MultipleHandlersTestCase.ThirdExecutedHandler; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
| import org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler; | ||
| import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; | ||
|
|
||
| // @formatter:off | ||
| // tag::user_guide[] | ||
| // Register handlers for @Test, @BeforeEach, @AfterEach as well as @BeforeAll and @AfterAll | ||
| @ExtendWith(ThirdExecutedHandler.class) | ||
| class MultipleHandlersTestCase { | ||
|
|
||
| // Register handlers for @Test, @BeforeEach, @AfterEach only | ||
| @ExtendWith(SecondExecutedHandler.class) | ||
| @ExtendWith(FirstExecutedHandler.class) | ||
| @Test | ||
| void testMethod() { | ||
| } | ||
|
|
||
| // end::user_guide[] | ||
|
|
||
| static class FirstExecutedHandler implements TestExecutionExceptionHandler { | ||
| @Override | ||
| public void handleTestExecutionException(ExtensionContext context, Throwable ex) | ||
| throws Throwable { | ||
| throw ex; | ||
| } | ||
| } | ||
|
|
||
| static class SecondExecutedHandler implements LifecycleMethodExecutionExceptionHandler { | ||
| @Override | ||
| public void handleBeforeEachMethodExecutionException(ExtensionContext context, Throwable ex) | ||
| throws Throwable { | ||
| throw ex; | ||
| } | ||
| } | ||
|
|
||
| static class ThirdExecutedHandler implements LifecycleMethodExecutionExceptionHandler { | ||
| @Override | ||
| public void handleBeforeAllMethodExecutionException(ExtensionContext context, Throwable ex) | ||
| throws Throwable { | ||
| throw ex; | ||
| } | ||
| } | ||
| // tag::user_guide[] | ||
| } | ||
| // end::user_guide[] | ||
| // @formatter:on | ||
55 changes: 55 additions & 0 deletions
55
documentation/src/test/java/example/exception/RecordStateOnErrorExtension.java
This file contains hidden or 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,55 @@ | ||
| /* | ||
| * Copyright 2015-2019 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.extension.ExtensionContext; | ||
| import org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler; | ||
|
|
||
| // @formatter:off | ||
| // tag::user_guide[] | ||
| class RecordStateOnErrorExtension implements LifecycleMethodExecutionExceptionHandler { | ||
|
|
||
| @Override | ||
| public void handleBeforeAllMethodExecutionException(ExtensionContext context, Throwable ex) | ||
| throws Throwable { | ||
| memoryDumpForFurtherInvestigation("Failure recorded during class setup"); | ||
| throw ex; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleBeforeEachMethodExecutionException(ExtensionContext context, Throwable ex) | ||
| throws Throwable { | ||
| memoryDumpForFurtherInvestigation("Failure recorded during test setup"); | ||
| throw ex; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleAfterEachMethodExecutionException(ExtensionContext context, Throwable ex) | ||
| throws Throwable { | ||
| memoryDumpForFurtherInvestigation("Failure recorded during test cleanup"); | ||
| throw ex; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleAfterAllMethodExecutionException(ExtensionContext context, Throwable ex) | ||
| throws Throwable { | ||
| memoryDumpForFurtherInvestigation("Failure recorded during class cleanup"); | ||
| throw ex; | ||
| } | ||
| // end::user_guide[] | ||
|
|
||
| private void memoryDumpForFurtherInvestigation(String error) { | ||
|
|
||
| } | ||
| // tag::user_guide[] | ||
| } | ||
pietrygamat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // end::user_guide[] | ||
| // @formatter:on | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.