Class SoftAssertionsExtension

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.AfterTestExecutionCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver

    public class SoftAssertionsExtension
    extends java.lang.Object
    implements org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.AfterTestExecutionCallback
    Extension for JUnit Jupiter that provides support for injecting a concrete implementation of SoftAssertionsProvider into test methods. Two examples that come packaged with AssertJ are SoftAssertions and BDDSoftAssertions, but custom implementations are also supported as long as they have a default constructor.

    Applicability

    In this context, the term "test method" refers to any method annotated with @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, or @TestTemplate.
    This extension does not inject SoftAssertionsProvider arguments into test constructors or lifecycle methods.

    Scope

    The scope of the SoftAssertionsProvider instance managed by this extension begins when a parameter of type SoftAssertionsProvider 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.

    Example with 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);
        }
     }
     

    Example with 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);
        }
     }
     
    Since:
    3.13
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void afterTestExecution​(org.junit.jupiter.api.extension.ExtensionContext extensionContext)  
      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)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SOFT_ASSERTIONS_EXTENSION_NAMESPACE

        private static final org.junit.jupiter.api.extension.ExtensionContext.Namespace SOFT_ASSERTIONS_EXTENSION_NAMESPACE
    • Constructor Detail

      • SoftAssertionsExtension

        public SoftAssertionsExtension()
    • Method Detail

      • supportsParameter

        public boolean supportsParameter​(org.junit.jupiter.api.extension.ParameterContext parameterContext,
                                         org.junit.jupiter.api.extension.ExtensionContext extensionContext)
        Specified by:
        supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      • resolveParameter

        public java.lang.Object resolveParameter​(org.junit.jupiter.api.extension.ParameterContext parameterContext,
                                                 org.junit.jupiter.api.extension.ExtensionContext extensionContext)
        Specified by:
        resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      • afterTestExecution

        public void afterTestExecution​(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
        Specified by:
        afterTestExecution in interface org.junit.jupiter.api.extension.AfterTestExecutionCallback
      • isUnsupportedParameterType

        private static boolean isUnsupportedParameterType​(java.lang.reflect.Parameter parameter)
      • getStore

        private static org.junit.jupiter.api.extension.ExtensionContext.Store getStore​(org.junit.jupiter.api.extension.ExtensionContext extensionContext)