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, org.junit.jupiter.api.extension.TestInstantiationAwareExtension

@Deprecated(since="3", forRemoval=true) public class SoftlyExtension extends Object implements org.junit.jupiter.api.extension.AfterTestExecutionCallback, org.junit.jupiter.api.extension.TestInstancePostProcessor
Deprecated, for removal: This API element is subject to removal in a future version.
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:

  • 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:

  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
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

    org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope
  • Constructor Summary

    Constructors
    Constructor
    Description
    Deprecated, for removal: This API element is subject to removal in a future version.
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
    Deprecated, for removal: This API element is subject to removal in a future version.
     
    void
    postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
    Deprecated, for removal: This API element is subject to removal in a future version.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

    getTestInstantiationExtensionContextScope
  • Constructor Details

    • SoftlyExtension

      public SoftlyExtension()
      Deprecated, for removal: This API element is subject to removal in a future version.
  • Method Details

    • postProcessTestInstance

      public void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
      Deprecated, for removal: This API element is subject to removal in a future version.
      Specified by:
      postProcessTestInstance in interface org.junit.jupiter.api.extension.TestInstancePostProcessor
      Throws:
      Exception
    • afterTestExecution

      public void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
      Deprecated, for removal: This API element is subject to removal in a future version.
      Specified by:
      afterTestExecution in interface org.junit.jupiter.api.extension.AfterTestExecutionCallback
      Throws:
      Exception