Class SoftlyExtension
java.lang.Object
org.assertj.core.api.junit.jupiter.SoftlyExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterTestExecutionCallback
,org.junit.jupiter.api.extension.Extension
,org.junit.jupiter.api.extension.TestInstancePostProcessor
@Deprecated
public class SoftlyExtension
extends Object
implements org.junit.jupiter.api.extension.AfterTestExecutionCallback, org.junit.jupiter.api.extension.TestInstancePostProcessor
Deprecated.
Extension for JUnit Jupiter that provides support for injecting an instance of
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:
- the test class lifecycle is
TestInstance.Lifecycle.PER_CLASS
(see explanation below). - multiple
SoftAssertions
fields are found - no
SoftAssertions
field is found
Detecting multiple SoftAssertions
fields is a best effort at the time of this writing, some cases are not detected.
Limitations:
- Cannot be used with test context that have
PER_CLASS
life cycle as the sameSoftAssertions
would be reused between tests. - May exhibit unpredictable behaviour in concurrent test execution
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
}
}
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.junit.jupiter.api.extension.ExtensionContext.Namespace
Deprecated. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
afterTestExecution
(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Deprecated.private static void
checkTooManySoftAssertionsFields
(Collection<Field> softAssertionsFields) Deprecated.private static Optional
<org.junit.jupiter.api.extension.ExtensionContext> Deprecated.private static org.junit.jupiter.api.extension.ExtensionContext.Store
getStore
(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Deprecated.private static Optional
<SoftAssertions> initSoftAssertionsField
(Object testInstance) Deprecated.private static boolean
isPerClassLifeCycle
(org.junit.jupiter.api.extension.ExtensionContext methodExtensionContext) Deprecated.void
postProcessTestInstance
(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext extensionContext) Deprecated.
-
Field Details
-
SOFTLY_EXTENSION_NAMESPACE
private static final org.junit.jupiter.api.extension.ExtensionContext.Namespace SOFTLY_EXTENSION_NAMESPACEDeprecated.
-
-
Constructor Details
-
SoftlyExtension
public SoftlyExtension()Deprecated.
-
-
Method Details
-
postProcessTestInstance
public void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception Deprecated.- Specified by:
postProcessTestInstance
in interfaceorg.junit.jupiter.api.extension.TestInstancePostProcessor
- Throws:
Exception
-
afterTestExecution
public void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception Deprecated.- Specified by:
afterTestExecution
in interfaceorg.junit.jupiter.api.extension.AfterTestExecutionCallback
- Throws:
Exception
-
getParent
private static Optional<org.junit.jupiter.api.extension.ExtensionContext> getParent(Optional<org.junit.jupiter.api.extension.ExtensionContext> currentContext) Deprecated. -
isPerClassLifeCycle
private static boolean isPerClassLifeCycle(org.junit.jupiter.api.extension.ExtensionContext methodExtensionContext) Deprecated. -
initSoftAssertionsField
private static Optional<SoftAssertions> initSoftAssertionsField(Object testInstance) throws IllegalAccessException Deprecated.- Throws:
IllegalAccessException
-
checkTooManySoftAssertionsFields
Deprecated. -
getStore
private static org.junit.jupiter.api.extension.ExtensionContext.Store getStore(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Deprecated.
-
SoftAssertionsExtension
as of AssertJ 3.18.0.