Package org.glassfish.tests.utils.junit
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 ownExtensionand add features.Injectable services:
@Inject- named after the test.Logger@Inject- install root and instance root are set to the root of test classpath; seeStartupContextgetStartupContextProperties(ExtensionContext).@InjectStaticModulesRegistry@InjectTestDocument- services configured by the
DomainXmlannotation.
- Author:
- David Matejcek
-
-
Constructor Summary
Constructors Constructor Description HK2JUnit5Extension()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddConstantServices(org.junit.jupiter.api.extension.ExtensionContext context)UsesServiceLocatorUtilities.addOneConstant(ServiceLocator, Object)calls to set some useful implicit services:@Inject- named after the test.Loggerprotected voidaddMockDescriptor(Class<?> iface)Creates a mock for the interface.voidafterAll(org.junit.jupiter.api.extension.ExtensionContext context)voidafterEach(org.junit.jupiter.api.extension.ExtensionContext context)voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context)voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context)protected org.glassfish.hk2.api.ServiceLocatorcreateLocator(org.junit.jupiter.api.extension.ExtensionContext context)protected List<Class<?>>getClasses(Class<?> testClass)protected ClassLoadergetClassLoader(org.junit.jupiter.api.extension.ExtensionContext context)protected StringgetDomainXml(Class<?> testClass)protected Class<? extends org.jvnet.hk2.config.DomDocument<?>>getDomainXmlDomClass(Class<?> testClass)protected Set<Class<?>>getExcludedClasses(Class<?> testClass)protected org.glassfish.hk2.api.ServiceLocatorgetLocator()protected Set<String>getLocatorFilePaths(org.junit.jupiter.api.extension.ExtensionContext context)protected StringgetLocatorName(org.junit.jupiter.api.extension.ExtensionContext context)protected List<String>getPackages(Class<?> testClass)protected PropertiesgetStartupContextProperties(org.junit.jupiter.api.extension.ExtensionContext context)voidpostProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context)
-
-
-
Method Detail
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback- Throws:
Exception
-
postProcessTestInstance
public void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
- Specified by:
postProcessTestInstancein interfaceorg.junit.jupiter.api.extension.TestInstancePostProcessor- Throws:
Exception
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Throws:
Exception
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Throws:
Exception
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
afterAllin interfaceorg.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:
ServiceLocatornamed bygetLocatorName(ExtensionContext)
-
getClassLoader
protected ClassLoader getClassLoader(org.junit.jupiter.api.extension.ExtensionContext context)
- Returns:
ClassLoaderof the test class
-
getStartupContextProperties
protected Properties getStartupContextProperties(org.junit.jupiter.api.extension.ExtensionContext context)
- Returns:
- properties for the
StartupContextinstance.
-
addConstantServices
protected void addConstantServices(org.junit.jupiter.api.extension.ExtensionContext context)
UsesServiceLocatorUtilities.addOneConstant(ServiceLocator, Object)calls to set some useful implicit services:@Inject- named after the test.Logger@InjectMockGenerator@Inject- install root and instance root are set to the root of test classpath; seeStartupContextgetStartupContextProperties(ExtensionContext).@InjectStaticModulesRegistry@InjectTestDocument
-
getDomainXml
protected String getDomainXml(Class<?> testClass)
- Parameters:
testClass-- Returns:
- path obtained from test's
DomainXmlannotation
-
getDomainXmlDomClass
protected Class<? extends org.jvnet.hk2.config.DomDocument<?>> getDomainXmlDomClass(Class<?> testClass)
- Parameters:
testClass-- Returns:
- a
DomDocumentclass obtained from test'sDomainXmlannotation
-
getPackages
protected List<String> getPackages(Class<?> testClass)
- Parameters:
testClass-- Returns:
- packages obtained from test's
Packagesannotation
-
getClasses
protected List<Class<?>> getClasses(Class<?> testClass)
- Parameters:
testClass-- Returns:
- classes obtained from test's
Classesannotation
-
getExcludedClasses
protected Set<Class<?>> getExcludedClasses(Class<?> testClass)
- Parameters:
testClass-- Returns:
- classes obtained from test's
ExcludeClassesannotation
-
getLocatorFilePaths
protected Set<String> getLocatorFilePaths(org.junit.jupiter.api.extension.ExtensionContext context)
- Parameters:
context-- Returns:
- paths obtained from test's
LocatorFilesannotation
-
addMockDescriptor
protected void addMockDescriptor(Class<?> iface)
Creates a mock for the interface. All it's methods will throwUnsupportedOperationException. The service then can be injected in cases when it is required as a dependency, but then unused.- Parameters:
iface-
-
-