public class SoftAssertionsExtension extends Object implements org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.AfterTestExecutionCallback
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| Constructor and Description |
|---|
SoftAssertionsExtension() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext) |
void |
beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) |
static AssertionErrorCollector |
getAssertionErrorCollector(org.junit.jupiter.api.extension.ExtensionContext context)
Returns the
AssertionErrorCollector for the given extension context, if none exists for the current context then
one is created. |
static <T extends SoftAssertionsProvider> |
getSoftAssertionsProvider(org.junit.jupiter.api.extension.ExtensionContext context,
Class<T> concreteSoftAssertionsProviderType)
Returns a
SoftAssertionsProvider instance of the given type for the given extension context. |
void |
postProcessTestInstance(Object testInstance,
org.junit.jupiter.api.extension.ExtensionContext context) |
Object |
resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext) |
boolean |
supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext) |
public void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
postProcessTestInstance in interface org.junit.jupiter.api.extension.TestInstancePostProcessorExceptionpublic void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
throws Exception
beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallbackExceptionpublic boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext)
supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolverpublic Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolverpublic void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
afterTestExecution in interface org.junit.jupiter.api.extension.AfterTestExecutionCallbackpublic static AssertionErrorCollector getAssertionErrorCollector(org.junit.jupiter.api.extension.ExtensionContext context)
AssertionErrorCollector for the given extension context, if none exists for the current context then
one is created.
This method is thread safe - all extensions attempting to access the AssertionErrorCollector for a given context
through this method will get a reference to the same AssertionErrorCollector instance, regardless of the order
in which they are called.
Third-party extensions that wish to provide soft-asserting behavior can use this method to obtain the current
AssertionErrorCollector instance and record their assertion failures into it by calling
collectAssertionError(AssertionError).
In this way their soft assertions will integrate with the existing AssertJ soft assertions and the assertion failures (both
AssertJ's and the third-party extension's) will be reported in the order that they occurred.
context - the ExtensionContext whose error collector we are attempting to retrieve.AssertionErrorCollector for the given context.public static <T extends SoftAssertionsProvider> T getSoftAssertionsProvider(org.junit.jupiter.api.extension.ExtensionContext context, Class<T> concreteSoftAssertionsProviderType)
SoftAssertionsProvider instance of the given type for the given extension context.
If no instance of the given type exists for the supplied context, then one is created.
This method is thread safe - all extensions attempting to access the SoftAssertionsProvider for a
given context through this method will receive end up getting a reference to the same
SoftAssertionsProvider instance of that same type, regardless of the order in which they are called.
Third party extensions that wish to use soft assertions in their own implementation can use this
to get a SoftAssertionsProvider instance that interoperates with other soft-asserting
extensions (including SoftAssertionsExtension).
The SoftAssertionExtension will take care of initialising this provider instance's delegate
at the appropriate time, so that collected soft assertions are routed to the AssertionErrorCollector
instance for the current context.
public class CustomExtension implements BeforeEachCallback {
@Override
public void beforeEach(ExtensionContext context) {
CustomSoftAssertions softly = SoftAssertionsExtension.getSoftAssertionsProvider(context, CustomSoftAssertions.class);
softly.assertThat(1).isOne();
}
}
T - the type of SoftAssertionsProvider to instantiate.context - the ExtensionContext whose error collector we are attempting to retrieve.concreteSoftAssertionsProviderType - the class instance for the type of soft assertionsAssertionErrorCollector for the given context.Copyright © 2025. All rights reserved.