public class SoftAssertionsExtension
extends java.lang.Object
implements org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.AfterTestExecutionCallback
SoftAssertions
or BDDSoftAssertions
into test methods.
In this context, the term "test method" refers to any method annotated with
@Test
, @RepeatedTest
, @ParameterizedTest
,
@TestFactory
, or @TestTemplate
.
This extension does not inject SoftAssertions
or BDDSoftAssertions
arguments into test
constructors or lifecycle methods.
The scope of the SoftAssertions
or BDDSoftAssertions
instance
managed by this extension begins when a parameter of type SoftAssertions
or BDDSoftAssertions
is resolved for a test method.
The scope of the instance ends after the test method has been executed, this is when
assertAll()
will be invoked on the instance to verify that no soft assertions failed.
SoftAssertions
@ExtendWith(SoftAssertionsExtension.class)
class ExampleTestCase {
@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);
}
}
BDDSoftAssertions
@ExtendWith(SoftAssertionsExtension.class)
class ExampleTestCase {
@Test
void multipleFailures(BDDSoftAssertions softly) {
softly.then(2 * 3).isEqualTo(0);
softly.then(Arrays.asList(1, 2)).containsOnly(1);
softly.then(1 + 1).isEqualTo(2);
}
}
Modifier and Type | Field and Description |
---|---|
private static org.junit.jupiter.api.extension.ExtensionContext.Namespace |
SOFT_ASSERTIONS_EXTENSION_NAMESPACE |
Constructor and Description |
---|
SoftAssertionsExtension() |
Modifier and Type | Method and Description |
---|---|
void |
afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext) |
private static <T> void |
assertAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext,
java.lang.Class<T> type,
java.util.function.Consumer<T> assertAll) |
private static org.junit.jupiter.api.extension.ExtensionContext.Store |
getStore(org.junit.jupiter.api.extension.ExtensionContext extensionContext) |
private static boolean |
isUnsupportedParameterType(java.lang.reflect.Parameter parameter) |
java.lang.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) |
private static final org.junit.jupiter.api.extension.ExtensionContext.Namespace SOFT_ASSERTIONS_EXTENSION_NAMESPACE
public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
supportsParameter
in interface org.junit.jupiter.api.extension.ParameterResolver
public java.lang.Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
resolveParameter
in interface org.junit.jupiter.api.extension.ParameterResolver
public void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
afterTestExecution
in interface org.junit.jupiter.api.extension.AfterTestExecutionCallback
private static <T> void assertAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext, java.lang.Class<T> type, java.util.function.Consumer<T> assertAll)
private static boolean isUnsupportedParameterType(java.lang.reflect.Parameter parameter)
private static org.junit.jupiter.api.extension.ExtensionContext.Store getStore(org.junit.jupiter.api.extension.ExtensionContext extensionContext)