Class LifecycleExtension
java.lang.Object
io.github.cjstehno.testthings.junit.LifecycleExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback
,org.junit.jupiter.api.extension.AfterEachCallback
,org.junit.jupiter.api.extension.BeforeAllCallback
,org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
public class LifecycleExtension
extends Object
implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterAllCallback
JUnit 5 extension used to provide a bit more control of the before/after lifecycle methods.
Extensions run before the local test instance before/after methods - this can lead to chicken-and-egg issues from
time to time. This extension allow you to define methods that will be run before and after, based on where this
extension is defined in the list of extensions.
For example, if you want to execute a specific before and after method before all of the extensions and before all
the other local lifecycle methods, you can configure this extension as the first one in the list, and then the
annotated method will execute before all of the other extensions, and then after all of the others at the end.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
afterAll
(org.junit.jupiter.api.extension.ExtensionContext context) void
afterEach
(org.junit.jupiter.api.extension.ExtensionContext context) Finds all the methods annotated with theLifecycle(AFTER)
annotation, and executes them.void
beforeAll
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeEach
(org.junit.jupiter.api.extension.ExtensionContext context) Finds all the methods annotated with theLifecycle(BEFORE)
annotation and executes them.
-
Constructor Details
-
LifecycleExtension
public LifecycleExtension()
-
-
Method Details
-
beforeAll
- Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
- Throws:
Exception
-
beforeEach
Finds all the methods annotated with theLifecycle(BEFORE)
annotation and executes them.- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Parameters:
context
- the current extension context; nevernull
- Throws:
Exception
- if there is a problem
-
afterEach
Finds all the methods annotated with theLifecycle(AFTER)
annotation, and executes them.- Specified by:
afterEach
in interfaceorg.junit.jupiter.api.extension.AfterEachCallback
- Parameters:
context
- the current extension context; nevernull
- Throws:
Exception
- if there is a problem
-
afterAll
- Specified by:
afterAll
in interfaceorg.junit.jupiter.api.extension.AfterAllCallback
- Throws:
Exception
-