Class HK2JUnit5Extension

  • 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.TestInstancePostProcessor

    public class HK2JUnit5Extension
    extends Object
    implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterAllCallback
    This JUnit5 extension allows to use HK2 services in tests. You can also override methods in your own Extension and add features.

    Injectable services:

    Author:
    David Matejcek
    • Constructor Detail

      • HK2JUnit5Extension

        public HK2JUnit5Extension()
    • Method Detail

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

        public void postProcessTestInstance​(Object testInstance,
                                            org.junit.jupiter.api.extension.ExtensionContext context)
                                     throws Exception
        Specified by:
        postProcessTestInstance in interface org.junit.jupiter.api.extension.TestInstancePostProcessor
        Throws:
        Exception
      • beforeEach

        public void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext context)
                        throws Exception
        Specified by:
        beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
        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
      • 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
      • getLocatorName

        protected String getLocatorName​(org.junit.jupiter.api.extension.ExtensionContext context)
        Returns:
        simple name of the test class + ServiceLocator
      • createLocator

        protected org.glassfish.hk2.api.ServiceLocator createLocator​(org.junit.jupiter.api.extension.ExtensionContext context)
        Returns:
        ServiceLocator named by getLocatorName(ExtensionContext)
      • getClassLoader

        protected ClassLoader getClassLoader​(org.junit.jupiter.api.extension.ExtensionContext context)
        Returns:
        ClassLoader of the test class
      • getStartupContextProperties

        protected Properties getStartupContextProperties​(org.junit.jupiter.api.extension.ExtensionContext context)
        Returns:
        properties for the StartupContext instance.
      • addConstantServices

        protected void addConstantServices​(org.junit.jupiter.api.extension.ExtensionContext context)
        Uses ServiceLocatorUtilities.addOneConstant(ServiceLocator, Object) calls to set some useful implicit services:
      • @Inject Logger - named after the test.
      • @Inject MockGenerator
      • @Inject StartupContext - install root and instance root are set to the root of test classpath; see getStartupContextProperties(ExtensionContext).
      • @Inject StaticModulesRegistry
      • @Inject TestDocument