Class WeldJunit5Extension

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver
    Direct Known Subclasses:
    WeldJunit5AutoExtension

    public class WeldJunit5Extension
    extends Object
    implements org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
    JUnit 5 extension allowing to bootstrap Weld SE container for each @Test method (or per once per test class if running TestInstance.Lifecycle.PER_CLASS) and tear it down afterwards. Also allows to inject CDI beans as parameters to @Test methods and resolves all @Inject fields in test class.

    If no WeldInitiator field annotated with WeldSetup is present on a test class, all service providers of WeldJunitEnricher interface are used to enrich the default test environment.

     @ExtendWith(WeldJunit5Extension.class)
     public class SimpleTest {
    
         // Injected automatically
         @Inject
         Foo foo;
    
         @Test
         public void testFoo() {
             // Weld container is started automatically
             assertEquals("baz", foo.getBaz());
         }
     }
     
    Author:
    Matej Novotny
    See Also:
    EnableWeld, WeldJunitEnricher
    • Constructor Detail

      • WeldJunit5Extension

        public WeldJunit5Extension()
    • Method Detail

      • afterAll

        public void afterAll​(org.junit.jupiter.api.extension.ExtensionContext context)
                      throws Exception
        Specified by:
        afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
        Throws:
        Exception
      • beforeAll

        public void beforeAll​(org.junit.jupiter.api.extension.ExtensionContext context)
                       throws Exception
        Specified by:
        beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
        Throws:
        Exception
      • afterEach

        public void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)
                       throws Exception
        Specified by:
        afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
        Throws:
        Exception
      • weldInit

        protected void weldInit​(org.junit.jupiter.api.extension.ExtensionContext context,
                                org.jboss.weld.environment.se.Weld weld,
                                WeldInitiator.Builder weldInitiatorBuilder)
      • resolveParameter

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

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

        public void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
                        throws Exception
        Specified by:
        beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
        Throws:
        Exception