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 own
Extension and 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
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final IntegerOne second timeout set as system property before tests start. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected 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) 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) getExcludedClasses(Class<?> testClass) protected org.glassfish.hk2.api.ServiceLocatorgetLocatorFilePaths(org.junit.jupiter.api.extension.ExtensionContext context) protected StringgetLocatorName(org.junit.jupiter.api.extension.ExtensionContext context) getPackages(Class<?> testClass) protected PropertiesgetStartupContextProperties(org.junit.jupiter.api.extension.ExtensionContext context) voidpostProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context)
-
Field Details
-
HK2_CONFIG_LOCK_TIME_OUT_IN_SECONDS
One second timeout set as system property before tests start.- See Also:
-
-
Constructor Details
-
HK2JUnit5Extension
public HK2JUnit5Extension()
-
-
Method Details
-
beforeAll
- 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
- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Throws:
Exception
-
afterEach
- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Throws:
Exception
-
afterAll
- 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
- 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
- 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
- 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
- Parameters:
testClass-- Returns:
- packages obtained from test's
Packagesannotation
-
getClasses
- Parameters:
testClass-- Returns:
- classes obtained from test's
Classesannotation
-
getExcludedClasses
- Parameters:
testClass-- Returns:
- classes obtained from test's
ExcludeClassesannotation
-
getLocatorFilePaths
- Parameters:
context-- Returns:
- paths obtained from test's
LocatorFilesannotation
-
addMockDescriptor
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-
-