Top Description Fields Constructors Methods
org.assertj.core.api.junit.jupiter

public Class SoftlyExtension

extends Object
implements AfterTestExecutionCallback, TestInstancePostProcessor
Class Inheritance
All Implemented Interfaces
org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.AfterTestExecutionCallback
Annotations
@Deprecated
Static Imports
org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS, org.junit.platform.commons.support.ReflectionSupport.findFields

Deprecated

This functionality (and more) has been rolled into SoftAssertionsExtension as of AssertJ 3.18.0.

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:

Detecting multiple SoftAssertions fields is a best effort at the time of this writing, some cases are not detected.

Limitations:

  1. Cannot be used with test context that have PER_CLASS life cycle as the same SoftAssertions would be reused between tests.
  2. 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
    }
  }
} 
Author
Arthur Mita

Field Summary

Modifier and TypeField and Description
private static final ExtensionContext.Namespace

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterTestExecution(ExtensionContext
the current extension context; never null
extensionContext
)

Implements org.junit.jupiter.api.extension.AfterTestExecutionCallback.afterTestExecution.

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.
private static void
private static Optional<ExtensionContext>
private static ExtensionContext.Store
getStore(ExtensionContext extensionContext)

private static Optional<SoftAssertions>
private static boolean
isPerClassLifeCycle(ExtensionContext methodExtensionContext)

public void
postProcessTestInstance(Object
the instance to post-process; never null
testInstance
,
ExtensionContext
the current extension context; never null
extensionContext
)

Implements org.junit.jupiter.api.extension.TestInstancePostProcessor.postProcessTestInstance.

Callback for post-processing the supplied test instance.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

SOFTLY_EXTENSION_NAMESPACEback to summary
private static final ExtensionContext.Namespace SOFTLY_EXTENSION_NAMESPACE

Constructor Detail

SoftlyExtensionback to summary
public SoftlyExtension()

Method Detail

afterTestExecutionback to summary
public void afterTestExecution(ExtensionContext extensionContext) throws Exception

Implements org.junit.jupiter.api.extension.AfterTestExecutionCallback.afterTestExecution.

Doc from org.junit.jupiter.api.extension.AfterTestExecutionCallback.afterTestExecution.

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.

Parameters
extensionContext:ExtensionContext

the current extension context; never null

Annotations
@Override
checkTooManySoftAssertionsFieldsback to summary
private static void checkTooManySoftAssertionsFields(Collection<Field> softAssertionsFields)
getParentback to summary
private static Optional<ExtensionContext> getParent(Optional<ExtensionContext> currentContext)
getStoreback to summary
private static ExtensionContext.Store getStore(ExtensionContext extensionContext)
initSoftAssertionsFieldback to summary
private static Optional<SoftAssertions> initSoftAssertionsField(Object testInstance) throws IllegalAccessException
isPerClassLifeCycleback to summary
private static boolean isPerClassLifeCycle(ExtensionContext methodExtensionContext)
postProcessTestInstanceback to summary
public void postProcessTestInstance(Object testInstance, ExtensionContext extensionContext) throws Exception

Implements org.junit.jupiter.api.extension.TestInstancePostProcessor.postProcessTestInstance.

Doc from org.junit.jupiter.api.extension.TestInstancePostProcessor.postProcessTestInstance.

Callback for post-processing the supplied test instance.

Note

the ExtensionContext supplied to a TestInstancePostProcessor will always return an empty java.util.Optional value from getTestInstance(). A TestInstancePostProcessor should therefore only attempt to process the supplied testInstance.

Parameters
testInstance:Object

the instance to post-process; never null

extensionContext:ExtensionContext

the current extension context; never null

Annotations
@Override