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
public class SoftlyExtension extends java.lang.Object implements org.junit.jupiter.api.extension.AfterTestExecutionCallback, org.junit.jupiter.api.extension.TestInstancePostProcessorExtension for JUnit Jupiter that provides support for injecting an instance ofSoftAssertionsinto a class testSoftAssertionsfield.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
SoftAssertionsfield when it extendsthisextension or can inherit the parent'sSoft assertions fieldThis extension throws an
IllegalStateExceptionif:- the test class lifecycle is
TestInstance.Lifecycle.PER_CLASS(see explanation below). - multiple
SoftAssertionsfields are found - no
SoftAssertionsfield is found
Detecting multiple
SoftAssertionsfields 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_CLASSlife cycle as the sameSoftAssertionswould be reused between tests. - May exhibit unpredictable behaviour in concurrent test execution
If you hit such limitations, consider using
SoftAssertionsExtensioninstead.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
Fields Modifier and Type Field Description private static org.junit.jupiter.api.extension.ExtensionContext.NamespaceSOFTLY_EXTENSION_NAMESPACE
-
Constructor Summary
Constructors Constructor Description SoftlyExtension()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext)private static voidcheckTooManySoftAssertionsFields(java.util.Collection<java.lang.reflect.Field> softAssertionsFields)private static java.util.Optional<org.junit.jupiter.api.extension.ExtensionContext>getParent(java.util.Optional<org.junit.jupiter.api.extension.ExtensionContext> currentContext)private static org.junit.jupiter.api.extension.ExtensionContext.StoregetStore(org.junit.jupiter.api.extension.ExtensionContext extensionContext)private static java.util.Optional<SoftAssertions>initSoftAssertionsField(java.lang.Object testInstance)private static booleanisPerClassLifeCycle(org.junit.jupiter.api.extension.ExtensionContext methodExtensionContext)voidpostProcessTestInstance(java.lang.Object testInstance, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
-
-
-
Method Detail
-
postProcessTestInstance
public void postProcessTestInstance(java.lang.Object testInstance, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws java.lang.Exception- Specified by:
postProcessTestInstancein interfaceorg.junit.jupiter.api.extension.TestInstancePostProcessor- Throws:
java.lang.Exception
-
afterTestExecution
public void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws java.lang.Exception- Specified by:
afterTestExecutionin interfaceorg.junit.jupiter.api.extension.AfterTestExecutionCallback- Throws:
java.lang.Exception
-
getParent
private static java.util.Optional<org.junit.jupiter.api.extension.ExtensionContext> getParent(java.util.Optional<org.junit.jupiter.api.extension.ExtensionContext> currentContext)
-
isPerClassLifeCycle
private static boolean isPerClassLifeCycle(org.junit.jupiter.api.extension.ExtensionContext methodExtensionContext)
-
initSoftAssertionsField
private static java.util.Optional<SoftAssertions> initSoftAssertionsField(java.lang.Object testInstance) throws java.lang.IllegalAccessException
- Throws:
java.lang.IllegalAccessException
-
checkTooManySoftAssertionsFields
private static void checkTooManySoftAssertionsFields(java.util.Collection<java.lang.reflect.Field> softAssertionsFields)
-
getStore
private static org.junit.jupiter.api.extension.ExtensionContext.Store getStore(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
-
-