public class SystemPropertyExtension extends Object implements org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback
System properties can be injected into your test or test case with either of the following approaches:
X which extends Y then you must add the annotation
to Class X not to Class Y. For example:
@SystemProperty(name = "nameA", value = "valueA")
public class MyTest {
// ...
}
@Test method. For example:
@Test
@SystemProperty(name = "nameA", value = "valueA")
public void testUsingSystemProperty() {
// ...
}
The SystemProperty annotation is repeatable.
Usage examples:
Declaring system properties at class level:
@SystemProperty(name = "nameA", value = "valueA")
@SystemProperty(name = "nameB", value = "valueB")
public class MyTest {
@Test
public void test() {
// the system properties nameA:valueA, nameB:valueB have been set
// ...
}
}
Declaring system properties at method level:
public class MyTest {
@Test
@SystemProperty(name = "nameA", value = "valueA")
@SystemProperty(name = "nameB", value = "valueB")
public void testUsingSystemProperties(TemporaryFolder temporaryFolder) {
// the system properties nameA:valueA, nameB:valueB have been set
// ...
}
@Test
@SystemProperty(name = "nameC", value = "valueC")
public void testUsingSystemProperty(TemporaryFolder temporaryFolder) {
// the system property nameC:valueC has been set
// ...
}
@Test
public void testWithoutSystemProperties() {
// the system properties nameA:valueA, nameB:valueB, nameC:valueC have *not* been set
// ...
}
}
| Constructor and Description |
|---|
SystemPropertyExtension() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
If a
RestoreContext exists for the given extensionContext then restore it i.e. |
void |
afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
If a
RestoreContext exists for the given extensionContext then restore it i.e. |
void |
beforeAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
If the current test class has a system property annotation(s) then create a
RestoreContext representing the annotation(s). |
void |
beforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
If the current test method has a system property annotation(s) then create a
RestoreContext representing the annotation(s). |
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
throws Exception
RestoreContext representing the annotation(s). This causes the requested system properties to
be set and retains a copy of pre-set values for reinstatement after test execution.beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallbackextensionContext - the context in which the current test or container is being
executedExceptionpublic void afterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
throws Exception
RestoreContext exists for the given extensionContext then restore it i.e.
unset any system properties which were set in beforeAll(ExtensionContext) for this
extensionContext and reinstate original value, if applicable.afterAll in interface org.junit.jupiter.api.extension.AfterAllCallbackextensionContext - the context in which the current test or container is being
executedExceptionpublic void beforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
throws Exception
RestoreContext representing the annotation(s). This causes the requested system properties to
be set and retains a copy of pre-set values for reinstatement after test execution.beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallbackextensionContext - the context in which the current test or container is being
executedExceptionpublic void afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
throws Exception
RestoreContext exists for the given extensionContext then restore it i.e.
unset any system properties which were set in beforeEach(ExtensionContext) for this
extensionContext and reinstate original value, if applicable.afterEach in interface org.junit.jupiter.api.extension.AfterEachCallbackextensionContext - the context in which the current test or container is being
executedExceptionCopyright © 2022. All rights reserved.