Deprecated
This functionality (and more) has been rolled into SoftAssertionsExtension
as of AssertJ 3.18.0.
SoftAssertions into a class test SoftAssertions field.
The injection occurs before each test method execution, after each test assertAll() is invoked to evaluate all test assertions.
A nested test class can provide a SoftAssertions field when it extends this extension or can inherit
the parent's Soft assertions field
This extension throws an IllegalStateException if:
Lifecycle#PER_CLASS (see explanation below).SoftAssertions fields are foundSoftAssertions field is found
Detecting multiple SoftAssertions fields is a best effort at the time of this writing, some cases are not detected.
Limitations:
PER_CLASS life cycle as the same SoftAssertions would be reused between tests.
If you hit such limitations, consider using SoftAssertionsExtension instead, since 3.18.0,
SoftAssertionsExtension supports field injection with neither of these two limitations.
Example:
@ExtendWith(SoftlyExtension.class)
public class SoftlyExtensionExample {
// initialized by the SoftlyExtension extension
private SoftAssertions soft;
@Test
public void chained_soft_assertions_example() {
String name = "Michael Jordan - Bulls";
soft.assertThat(name)
.startsWith("Mi")
.contains("Bulls");
// no need to call softly.assertAll(), this is done by the extension
}
// nested classes test work too
@Nested
class NestedExample {
@Test
public void football_assertions_example() {
String kylian = "Kylian Mbappé";
soft.assertThat(kylian)
.startsWith("Ky")
.contains("bap");
// no need to call softly.assertAll(), this is done by the extension
}
}
}
| Modifier and Type | Field and Description |
|---|---|
| private static final ExtensionContext. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | afterTestExecution(ExtensionContext
the current extension context; never extensionContext)nullImplements org. |
| private static void | |
| private static Optional | |
| private static ExtensionContext. | |
| private static Optional | |
| private static boolean | |
| public void | postProcessTestInstance(Object
the instance to post-process; never testInstance, ExtensionContext nullthe current extension context; never extensionContext)nullImplements org. |
| SOFTLY_EXTENSION_NAMESPACE | back to summary |
|---|---|
| private static final ExtensionContext. | |
| SoftlyExtension | back to summary |
|---|---|
| public SoftlyExtension() | |
| afterTestExecution | back to summary |
|---|---|
| public void afterTestExecution(ExtensionContext extensionContext) throws Exception Implements org. Doc from org. Callback that is invoked immediately after an individual test has been executed but before any user-defined teardown methods have been executed for that test.
| |
| checkTooManySoftAssertionsFields | back to summary |
|---|---|
| private static void checkTooManySoftAssertionsFields(Collection<Field> softAssertionsFields) | |
| getParent | back to summary |
|---|---|
| private static Optional | |
| getStore | back to summary |
|---|---|
| private static ExtensionContext. | |
| initSoftAssertionsField | back to summary |
|---|---|
| private static Optional | |
| isPerClassLifeCycle | back to summary |
|---|---|
| private static boolean isPerClassLifeCycle(ExtensionContext methodExtensionContext) | |
| postProcessTestInstance | back to summary |
|---|---|
| public void postProcessTestInstance(Object testInstance, ExtensionContext extensionContext) throws Exception Implements org. Doc from org. Callback for post-processing the supplied test instance. Note the
| |