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 Object
    implements org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.AfterTestExecutionCallback
    Extension for JUnit Jupiter that provides support for injecting an instance of SoftAssertions or BDDSoftAssertions into test methods.

    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 SoftAssertions or BDDSoftAssertions arguments into test constructors or lifecycle methods.

    Scope

    The scope of the SoftAssertions or BDDSoftAssertions instance managed by this extension begins when a parameter of type SoftAssertions or BDDSoftAssertions 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
    Author:
    Sam Brannen
    • 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 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
      • assertAll

        private static <T> void assertAll​(org.junit.jupiter.api.extension.ExtensionContext extensionContext,
                                          Class<T> type,
                                          Consumer<T> assertAll)
      • isUnsupportedParameterType

        private static boolean isUnsupportedParameterType​(Parameter parameter)
      • getStore

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