SoftAssertionsProvider
into test methods and (since 3.18.0) into test fields annotated with @InjectSoftAssertions.
Two examples of SoftAssertionsProviders that come packaged with AssertJ are SoftAssertions and
BDDSoftAssertions, but custom implementations are also supported as long as they are non-abstract and have a default
constructor.
In this context, the term "test method" refers to any method annotated with @Test, @RepeatedTest,
@ParameterizedTest, @TestFactory, or @TestTemplate.
This extension does not inject SoftAssertionsProvider arguments into test constructors or lifecycle methods.
SoftAssertionsProvider fields become valid from the `@BeforeEach` lifecycle phase.
For parameters, they become are valid when the parameter is resolved.afterTestExecution phase (immediately after the test has returned, but before the AfterEach phase, all
collected errors (if any) will wrapped in a single multiple-failures error.SoftAssertionsProvider instances (fields & parameters) created within the scope of the same test method
(including its BeforeEach phase) will share the same state object to collect the failed assertions, so that all
assertion failures from all SoftAssertionsProviders will be reported in the order that they failed.
SoftAssertionExtension's API.
Calling getAssertionErrorCollector(ExtensionContext) will return a handle to the error collector used for the current
context into which a third-party extension can directly store its assertion failures. Alternatively, calling
getSoftAssertionsProvider() will instantiate a
SoftAssertionsProvider for the given context that can then be used to make assertions.
@ExtendWith(SoftAssertionsExtension.class)
class ExampleTestCase {
@InjectSoftAssertions
BDDSoftAssertions bdd;
@Test
void multipleFailures(SoftAssertions softly) {
softly.assertThat(2 * 3).isEqualTo(0);
softly.assertThat(Arrays.asList(1, 2)).containsOnly(1);
softly.assertThat(1 + 1).isEqualTo(2);
}
}
@ExtendWith(SoftlyExtension.class)
public class SoftlyExtensionExample {
// initialized by the SoftlyExtension extension
@InjectSoftAssertions
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
}
}
}
@ExtendWith(SoftAssertionsExtension.class)
class ExampleTestCase {
@InjectSoftAssertions
SoftAssertions softly
@Test
void multipleFailures(BDDSoftAssertions bdd) {
bdd.then(2 * 3).isEqualTo(0);
softly.assertThat(Arrays.asList(1, 2)).containsOnly(1);
bdd.then(1 + 1).isEqualTo(2);
// When SoftAssertionsExtension calls assertAll(), the three
// above failures above will be reported in-order.
}
}
SoftAssertionsExtension
class ExampleTestCase implements BeforeEachCallback {
@Override
public void beforeEach(ExtensionContext context) {
SoftAssertions softly = SoftAssertionsExtension
.getSoftAssertionsProvider(context, SoftAssertions.class);
softly.assertThat(false).isTrue();
// When SoftAssertionsExtension calls assertAll(), the
// above failure will be included in the list of reported failures.
}
}
SoftlyExtension), Fr Jeremy Krieg
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| 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 Class | |
| public void | beforeEach(ExtensionContext
the current extension context; never context)nullImplements org. |
| private static void | checkHasDefaultConstructor(Field softAssertionsField, Class<? extends SoftAssertionsProvider> softAssertionsProviderClass)
|
| private static void | checkIsNotAbstract(Field softAssertionsField, Class<? extends SoftAssertionsProvider> softAssertionsProviderClass)
|
| private static void | |
| public static AssertionErrorCollector | Returns: TheAssertionErrorCollector for the given context.the context)ExtensionContext whose error collector we are attempting to retrieve.Returns the |
| public static < the type of T extends SoftAssertionsProvider> TSoftAssertionsProvider to instantiate. | Returns: TheAssertionErrorCollector for the given context.the context, Class<T> ExtensionContext whose error collector we are attempting to retrieve.the class instance for the type of soft assertions concreteSoftAssertionsProviderType)Returns a |
| private static Collection | |
| private static ExtensionContext. | |
| private static SoftAssertionsExtension. | |
| private static boolean | |
| private static <T extends SoftAssertionsProvider> T | |
| pack-priv static boolean | |
| pack-priv static boolean | |
| pack-priv static boolean | |
| private static boolean | |
| public void | postProcessTestInstance(Object
the instance to post-process; never testInstance, ExtensionContext nullthe current extension context; never context)nullImplements org. |
| public Object | resolveParameter(ParameterContext
the context for the parameter for which an argument should
be resolved; never parameterContext, ExtensionContext nullthe extension context for the extensionContext)Executable
about to be invoked; never nullImplements org. Parameter in the supplied ParameterContext
for the supplied ExtensionContext.
|
| private static void | setTestInstanceSoftAssertionsField(Object testInstance, Field softAssertionsField, SoftAssertionsProvider softAssertions)
|
| public boolean | supportsParameter(ParameterContext
the context for the parameter for which an argument should
be resolved; never parameterContext, ExtensionContext nullthe extension context for the extensionContext)Executable
about to be invoked; never nullImplements org. Parameter in the supplied ParameterContext for the supplied
ExtensionContext.
|
| SOFT_ASSERTIONS_EXTENSION_NAMESPACE | back to summary |
|---|---|
| private static final ExtensionContext. | |
| SoftAssertionsExtension | back to summary |
|---|---|
| public SoftAssertionsExtension() | |
| afterTestExecution | back to summary |
|---|---|
| public void afterTestExecution(ExtensionContext extensionContext) 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.
| |
| asSoftAssertionsProviderClass | back to summary |
|---|---|
private static Class
| |
| beforeEach | back to summary |
|---|---|
| public void beforeEach(ExtensionContext context) throws Exception Implements org. Doc from org. Callback that is invoked before an individual test and any user-defined setup methods for that test have been executed.
| |
| checkHasDefaultConstructor | back to summary |
|---|---|
| private static void checkHasDefaultConstructor(Field softAssertionsField, Class<? extends SoftAssertionsProvider> softAssertionsProviderClass) | |
| checkIsNotAbstract | back to summary |
|---|---|
| private static void checkIsNotAbstract(Field softAssertionsField, Class<? extends SoftAssertionsProvider> softAssertionsProviderClass) | |
| checkIsNotStaticOrFinal | back to summary |
|---|---|
| private static void checkIsNotStaticOrFinal(Field softAssertionsField) | |
| getAssertionErrorCollector | back to summary |
|---|---|
| public static AssertionErrorCollector getAssertionErrorCollector(ExtensionContext context) Returns the
This method is thread safe - all extensions attempting to access the
Third-party extensions that wish to provide soft-asserting behavior can use this method to obtain the current
| |
| getSoftAssertionsProvider | back to summary |
|---|---|
| public static <T extends SoftAssertionsProvider> T getSoftAssertionsProvider(ExtensionContext context, Class<T> concreteSoftAssertionsProviderType) Returns a
This method is thread safe - all extensions attempting to access the
Third party extensions that wish to use soft assertions in their own implementation can use this
to get a
The
| |
| getSoftAssertionsProviders | back to summary |
|---|---|
private static Collection
| |
| getStore | back to summary |
|---|---|
| private static ExtensionContext. | |
| getThreadLocalCollector | back to summary |
|---|---|
| private static SoftAssertionsExtension. | |
| initialiseDelegate | back to summary |
|---|---|
| private static boolean initialiseDelegate(ExtensionContext context, AssertionErrorCollector collector) | |
| instantiateProvider | back to summary |
|---|---|
| private static <T extends SoftAssertionsProvider> T instantiateProvider(ExtensionContext context, Class<T> providerType) | |
| isAnnotatedConcurrent | back to summary |
|---|---|
| pack-priv static boolean isAnnotatedConcurrent(ExtensionContext context) | |
| isPerClass | back to summary |
|---|---|
| pack-priv static boolean isPerClass(ExtensionContext context) | |
| isPerClassConcurrent | back to summary |
|---|---|
| pack-priv static boolean isPerClassConcurrent(ExtensionContext context) | |
| isUnsupportedParameterType | back to summary |
|---|---|
| private static boolean isUnsupportedParameterType(Parameter parameter) | |
| postProcessTestInstance | back to summary |
|---|---|
| public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception Implements org. Doc from org. Callback for post-processing the supplied test instance. Note the
| |
| resolveParameter | back to summary |
|---|---|
| public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Implements org. Doc from org. Resolve an argument for the This method is only called by the framework if The
| |
| setTestInstanceSoftAssertionsField | back to summary |
|---|---|
| private static void setTestInstanceSoftAssertionsField(Object testInstance, Field softAssertionsField, SoftAssertionsProvider softAssertions) | |
| supportsParameter | back to summary |
|---|---|
| public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Implements org. Doc from org. Determine if this resolver supports resolution of an argument for the
The
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv InheritableThreadLocal |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public List | |
| public void | collectAssertionError(AssertionError
the assertionError)AssertionError to collect.Implements org. |
| public Optional | |
| public void | |
| public void | setDelegate(AssertionErrorCollector
the assertionErrorCollector)AssertionErrorCollector to which the assertions will be forwarded.Overrides default org. |
| public void | succeeded()
Implements org. |
| public boolean | wasSuccess()
Implements org. |
| threadLocal | back to summary |
|---|---|
| pack-priv InheritableThreadLocal<AssertionErrorCollector> threadLocal | |
| ThreadLocalErrorCollector | back to summary |
|---|---|
| pack-priv ThreadLocalErrorCollector() | |
| assertionErrorsCollected | back to summary |
|---|---|
| public List Implements org.
| |
| collectAssertionError | back to summary |
|---|---|
| public void collectAssertionError(AssertionError assertionError) Implements org. Doc from org. This method can be used to collect soft assertion errors.
To be able to react after an assertion error is collected, use
| |
| getDelegate | back to summary |
|---|---|
| public Optional Overrides default org.
| |
| reset | back to summary |
|---|---|
| public void reset() | |
| setDelegate | back to summary |
|---|---|
| public void setDelegate(AssertionErrorCollector assertionErrorCollector) Overrides default org. Doc from org. Optionally sets a "delegate" collector into which the collected assertions will be deposited. Note that if you set a delegate, this instance will no longer collect or report assertion errors itself but will forward them all to the delegate for collection.
| |
| succeeded | back to summary |
|---|---|
| public void succeeded() Implements org. Doc from org. Indicates/sets that the last assertion was a success.
| |
| wasSuccess | back to summary |
|---|---|
| public boolean wasSuccess() Implements org. Doc from org. Returns the result of last soft assertion which can be used to decide what the next one should be. Example:
| |