Class HK2JUnit5Extension

java.lang.Object
org.glassfish.tests.utils.junit.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 Details

    • HK2JUnit5Extension

      public HK2JUnit5Extension()
  • Method Details

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

      protected org.glassfish.hk2.api.ServiceLocator getLocator()
      Returns:
      locator with a name set by getLocatorName(ExtensionContext)
    • 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
    • getDomainXml

      protected String getDomainXml(Class<?> testClass)
      Parameters:
      testClass -
      Returns:
      path obtained from test's DomainXml annotation
    • getDomainXmlDomClass

      protected Class<? extends org.jvnet.hk2.config.DomDocument<?>> getDomainXmlDomClass(Class<?> testClass)
      Parameters:
      testClass -
      Returns:
      a DomDocument class obtained from test's DomainXml annotation
    • getPackages

      protected List<String> getPackages(Class<?> testClass)
      Parameters:
      testClass -
      Returns:
      packages obtained from test's Packages annotation
    • getClasses

      protected List<Class<?>> getClasses(Class<?> testClass)
      Parameters:
      testClass -
      Returns:
      classes obtained from test's Classes annotation
    • getExcludedClasses

      protected Set<Class<?>> getExcludedClasses(Class<?> testClass)
      Parameters:
      testClass -
      Returns:
      classes obtained from test's ExcludeClasses annotation
    • getLocatorFilePaths

      protected Set<String> getLocatorFilePaths(org.junit.jupiter.api.extension.ExtensionContext context)
      Parameters:
      context -
      Returns:
      paths obtained from test's LocatorFiles annotation
    • addMockDescriptor

      protected void addMockDescriptor(Class<?> iface)
      Creates a mock for the interface. All it's methods will throw UnsupportedOperationException. The service then can be injected in cases when it is required as a dependency, but then unused.
      Parameters:
      iface -