Class ServiceLocatorTestRule<T>
- Type Parameters:
T- the type of JUnit test thisServiceLocatorTestRuleis related to; consider making it an instance ofBinder
ExternalResource (and a Binder) that sets up and
tears down an HK2 ServiceLocator on a per-test-class or
per-test-method basis.- Since:
- 2.4.0-b33
- Author:
- Laird Nelson
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAnenumdescribing possibleServiceLocatorisolation levels for JUnit tests. -
Constructor Summary
ConstructorsConstructorDescriptionServiceLocatorTestRule(T test) Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTisolation in non-verbose mode.ServiceLocatorTestRule(T test, boolean verbose) Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTisolation with the supplied verbosity.ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation) Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolationin non-verbose mode.ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation, boolean verbose) Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolation. -
Method Summary
Modifier and TypeMethodDescriptionvoidafter()Shuts down and cleans up theServiceLocatorcreated by thisServiceLocatorTestRule.apply(Statement statement, Description description) Overrides theExternalResource.apply(Statement, Description)method to save the suppliedDescriptionso that other methods in this class can refer to it.voidbefore()When necessary, calls thecreateServiceLocator(Description),configureServiceLocator(ServiceLocator, Description)andperformDependencyInjection(ServiceLocator, Object)methods in order to set up aServiceLocatorappropriate for the current JUnit test.voidbind(DynamicConfiguration dynamicConfiguration) Called at the appropriate time by theconfigureServiceLocator(ServiceLocator, Description)method in case subclasses wish to add services to theServiceLocatorbeing configured.protected voidconfigureServiceLocator(ServiceLocator serviceLocator, Description testDescription) Configures the suppliedServiceLocatorfor use by the test described by the suppliedDescription.protected ServiceLocatorcreateServiceLocator(Description testDescription) Creates and returns aServiceLocatorsuitable for the JUnit test described by the suppliedDescription.protected StringReturns aStringrepresenting the classpath to use to search for classes.Returns theServiceLocatorTestRule.ServiceLocatorIsolationfor thisServiceLocatorTestRule.protected StringgetServiceLocatorName(Description testDescription) Returns a name for aServiceLocatorthat is appropriate for the suppliedDescription.final booleanprotected voidperformDependencyInjection(ServiceLocator serviceLocator, T test) Performs HK2 dependency injection on thisServiceLocatorTestRuleand the supplied test instance, using the suppliedServiceLocatoras needed.voidshutdownAndDestroyServiceLocator(Description testDescription) Blindly calls theServiceLocator.shutdown()andServiceLocatorFactory.destroy(ServiceLocator)methods on thisServiceLocatorTestRule's associatedServiceLocatorif it is discovered to be non-null.
-
Constructor Details
-
ServiceLocatorTestRule
Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTisolation in non-verbose mode.- Parameters:
test- the JUnit test; must not benull- Throws:
AssertionError- iftestisnull- See Also:
-
ServiceLocatorTestRule
Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTisolation with the supplied verbosity.- Parameters:
test- the JUnit test; must not benullverbose- whether thisServiceLocatorTestRuleshould output additional information toSystem.out- Throws:
AssertionError- iftestisnull- See Also:
-
ServiceLocatorTestRule
Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolationin non-verbose mode.- Parameters:
test- the JUnit test; must not benullisolation- theServiceLocatorTestRule.ServiceLocatorIsolation; ifnullthenServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTwill be used instead- Throws:
AssertionError- iftestisnull- See Also:
-
ServiceLocatorTestRule
public ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation, boolean verbose) Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolation.- Parameters:
test- the JUnit test; must not benullisolation- theServiceLocatorTestRule.ServiceLocatorIsolation; ifnullthenServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTwill be used insteadverbose- whether thisServiceLocatorTestRuleshould output additional information toSystem.out- Throws:
AssertionError- iftestisnull
-
-
Method Details
-
getServiceLocatorIsolation
Returns theServiceLocatorTestRule.ServiceLocatorIsolationfor thisServiceLocatorTestRule.This method never returns
null.- Returns:
- the
ServiceLocatorTestRule.ServiceLocatorIsolationfor thisServiceLocatorTestRule; nevernull
-
isVerbose
public final boolean isVerbose()- Returns:
trueif thisServiceLocatorTestRuleshould be verbose;falseotherwise- See Also:
-
before
When necessary, calls thecreateServiceLocator(Description),configureServiceLocator(ServiceLocator, Description)andperformDependencyInjection(ServiceLocator, Object)methods in order to set up aServiceLocatorappropriate for the current JUnit test.- Overrides:
beforein classExternalResource- Throws:
IOException- if theconfigureServiceLocator(ServiceLocator, Description)method threw anIOException- See Also:
-
configureServiceLocator
protected void configureServiceLocator(ServiceLocator serviceLocator, Description testDescription) throws IOException Configures the suppliedServiceLocatorfor use by the test described by the suppliedDescription.This implementation:
- Adds the
ErrorServiceImplclass to the suppliedServiceLocatorso that exceptions will be thrown from tests - Removes, via
DynamicConfiguration.addUnbindFilter(Filter), allDescriptors that haveDescription.classas one of their contracts - Adds a constant
descriptor in
Singletonscope with a name equal to the return value of theDescription.getDisplayName()method for the suppliedDescriptionso that the current test can inject theDescriptionfor the current method if it wishes - Adds any classes found in an optional
Classesannotation decorating the test class—if they are assignable toFactory.classthen they are added as factories - Adds any classes found in any packages listed in an optional
Packagesannotation decorating the test class, provided they are annotated withService - Reads any locator files listed in an optional
InhabitantFilesannotation decorating the test class and adds the services listed therein - Calls the
bind(DynamicConfiguration)method - Calls the
Binder.bind(DynamicConfiguration)method on the test if it is in fact an instance ofBinder
- Parameters:
serviceLocator- theServiceLocatorto configure; must not benulltestDescription- theDescriptiondescribing the particular test being run; must not benull- Throws:
AssertionError- if eitherserviceLocatorortestDescriptionisnullIOException- if there was an error looking for classes in packages or finding or reading locator files
- Adds the
-
bind
Called at the appropriate time by theconfigureServiceLocator(ServiceLocator, Description)method in case subclasses wish to add services to theServiceLocatorbeing configured.Overrides of this method must not invoke the
DynamicConfiguration.commit()method.The default implementation of this method does nothing.
- Specified by:
bindin interfaceBinder- Parameters:
dynamicConfiguration- theDynamicConfigurationto manipulate in order to add or remove services from theServiceLocatorbeing configured; must not benull
-
getClasspath
Returns aStringrepresenting the classpath to use to search for classes.This method never returns
null.Overrides of this method are permitted to return
null.The default implementation of this method returns the value of the
java.class.pathsystem property, or in the almost certainly catastrophic case where this is not set, the empty string.- Returns:
- a
Stringrepresenting the classpath, ornull
-
performDependencyInjection
Performs HK2 dependency injection on thisServiceLocatorTestRuleand the supplied test instance, using the suppliedServiceLocatoras needed.The default implementation of this method does the following:
- Calls
ServiceLocator.inject(Object)on the suppliedServiceLocator, if it is non-null, passing it thisServiceLocatorTestRule - Calls
ServiceLocator.postConstruct(Object)on the suppliedServiceLocator, if it is non-null, passing it thisServiceLocatorTestRule - Calls
ServiceLocator.inject(Object)on the suppliedServiceLocator, if it is non-null, passing it the suppliedtestinstance, if it is non-null - Calls
ServiceLocator.postConstruct(Object)on the suppliedServiceLocator, if it is non-null, passing it the suppliedtestinstance, if it is non-null
This method is guaranteed to be called after the
configureServiceLocator(ServiceLocator, Description)method with the sameServiceLocator.- Parameters:
serviceLocator- theServiceLocatorto use to perform injection; may benullin which case no action will be takentest- the test instance to inject; may benull- See Also:
- Calls
-
createServiceLocator
Creates and returns aServiceLocatorsuitable for the JUnit test described by the suppliedDescription.This method never returns
null.Overrides of this method must not return
null.- Parameters:
testDescription- theDescriptiondescribing the currently executing JUnit test; must not benull- Returns:
- a non-
nullServiceLocator - Throws:
AssertionError- iftestDescriptionisnull
-
getServiceLocatorName
Returns a name for aServiceLocatorthat is appropriate for the suppliedDescription.This method never returns
null.Overrides of this method must not return
null.- Parameters:
testDescription- theDescriptiondescribing the currently executing JUnit test; must not benull- Returns:
- a non-
nullStringthat will be used as a name for aServiceLocator - Throws:
AssertionError- iftestDescriptionisnull, or if itsgetClassName()method returnsnullor if itsgetMethodName()method returnsnull
-
apply
Overrides theExternalResource.apply(Statement, Description)method to save the suppliedDescriptionso that other methods in this class can refer to it.This method never returns
null.Overrides of this method must not return
null.- Specified by:
applyin interfaceTestRule- Overrides:
applyin classExternalResource- Parameters:
statement- theStatementthisServiceLocatorTestRulewill modify; passed unchanged to theExternalResource.apply(Statement, Description)methoddescription- theDescriptiondescribing the current JUnit test; must not benull; passed unchanged to theExternalResource.apply(Statement, Description)method- Returns:
- the return value of the
ExternalResource.apply(Statement, Description)method; nevernull
-
after
public void after()Shuts down and cleans up theServiceLocatorcreated by thisServiceLocatorTestRule.This method performs the following actions in order:
- Calls the
ServiceLocator.preDestroy(Object)method on the existingServiceLocator, passing it the test instance supplied at construction time - Calls the
ServiceLocator.preDestroy(Object)method on the existingServiceLocator, passing it thisServiceLocatorTestRule - If the
ServiceLocatorisolation level isPER_TEST, calls theshutdownAndDestroyServiceLocator(Description)method with aDescriptioninstance that describes the JUnit test method that just executed
- Overrides:
afterin classExternalResource- See Also:
- Calls the
-
shutdownAndDestroyServiceLocator
Blindly calls theServiceLocator.shutdown()andServiceLocatorFactory.destroy(ServiceLocator)methods on thisServiceLocatorTestRule's associatedServiceLocatorif it is discovered to be non-null.- Parameters:
testDescription- aDescriptiondescribing the currently executing JUnit test, if any; ignored by this method's default implementation; may benull
-