public abstract class SpincastTestBase extends Object implements BeforeAfterClassMethodsProvider, TestFailureListener, RepeatedClassAfterMethodProvider
Uses a custom Junit runner,
SpincastJUnitRunner.
This runner create a single instance of the test class for all of its tests,
instead of a new instance for each test. It also calls a beforeClass()
method before the tests are run and an afterClass() method after
they are run.
A class extending this will be part of a Guice context
(created using the createInjector method) and
the required dependencies will be injected into it.
The NAME_ASCENDING option is used to sort the tests. This means you can force the order in which tests are run by prefixeing them with something like : "t01_firstTest", "t02_secondTest", etc.
A GuiceTweaker instance is used to
be able to tweak a Guice context automagaically. This for example
allows you to start your actual application, using its main() method,
but still be able to mock some components for testing purposes.
Note that the Guice tweaker only works when the Guice context is created
using the standard Spincast bootstrapper.
| Modifier and Type | Field and Description |
|---|---|
protected org.slf4j.Logger |
logger |
| Constructor and Description |
|---|
SpincastTestBase() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addExtraSystemProperties()
Adds System properties as they are provided by the
getExtraSystemProperties() method. |
void |
afterClass()
Called after the tests of the class are ran.
|
void |
afterClassLoops()
Called after all the loops of the class
as specified by the @repeat annotation.
|
void |
afterTest() |
void |
beforeClass()
Called before the tests of the class are ran.
|
void |
beforeClassException(Throwable ex)
This method will be called if an exception occures during
the
BeforeAfterClassMethodsProvider.beforeClass() execution. |
void |
beforeTest() |
protected GuiceTweaker |
createGuiceTweaker() |
protected abstract com.google.inject.Injector |
createInjector()
The test class must implement this method to create
the Guice injector.
|
protected String |
createTestingFilePath()
Create a temporary test file.
|
protected String |
createTestingFilePath(String relativePath)
Create a temporary test file, using the given relative path.
|
protected void |
deleteTempDir() |
protected Map<String,String> |
getExtraSystemProperties()
Extra System properties to set before the tests are run.
|
protected Class<? extends SpincastConfig> |
getGuiceTweakerConfigImplementationClass()
The implementation to use for the
SpincastConfig binding
when isGuiceTweakerAutoTestingConfigBindings() is enabled. |
protected GuiceTweaker |
getGuiceTweakerFromThreadLocal() |
protected com.google.inject.Module |
getGuiceTweakerOverridingModule()
If an overriding Module is to be added using the
Guice tweaker.
|
protected List<SpincastPlugin> |
getGuiceTweakerPlugins()
Extra plugins to be added by the Guice Tweaker.
|
protected com.google.inject.Injector |
getInjector() |
protected SpincastConfig |
getSpincastConfig() |
protected File |
getTestingWritableDir() |
protected boolean |
isGuiceTweakerAutoTestingConfigBindings()
Should the SpincastConfig bindings be automatically
configured? If
true (the default),
any bindings for components in the hierarchy of SpincastConfig
will be removed and new bindings will be added using the
testing implementation returned by getGuiceTweakerConfigImplementationClass(). |
protected void |
resetSystemProperties()
Resets System properties.
|
protected void |
setSpincastConfig(SpincastConfig spincastConfig) |
void |
testFailure(org.junit.runner.notification.Failure failure)
You can override this method to be
informed when a test fails.
|
protected void |
validateCreatedInjector(com.google.inject.Injector guice)
Validates the created Injector, before the
dependencies are injected in the test class.
|
public void beforeClass()
BeforeAfterClassMethodsProviderbeforeClass in interface BeforeAfterClassMethodsProviderprotected void addExtraSystemProperties()
getExtraSystemProperties() method.protected Map<String,String> getExtraSystemProperties()
protected void resetSystemProperties()
protected GuiceTweaker getGuiceTweakerFromThreadLocal()
protected void validateCreatedInjector(com.google.inject.Injector guice)
protected GuiceTweaker createGuiceTweaker()
protected List<SpincastPlugin> getGuiceTweakerPlugins()
Most of the time, you want to make sure you keep the plugins already added by base classes. For example :
List<SpincastPlugin> plugins = super.getGuiceTweakerPlugins();
plugins.add(new YourPlugin());
return plugins;
protected com.google.inject.Module getGuiceTweakerOverridingModule()
In general, you want to keep the Module from the parent by combining/overriding it with your custom Module. For example :
return Modules.combine(super.getGuiceTweakerOverridingModule(),
new SpincastGuiceModuleBase() {
protected boolean isGuiceTweakerAutoTestingConfigBindings()
true (the default),
any bindings for components in the hierarchy of SpincastConfig
will be removed and new bindings will be added using the
testing implementation returned by getGuiceTweakerConfigImplementationClass().
In a typical Spincast application, those bindings will be removed :
If false, it is your responsibility to make
sure valid testing configurations bindings are used to create the
Guuice context.
protected Class<? extends SpincastConfig> getGuiceTweakerConfigImplementationClass()
SpincastConfig binding
when isGuiceTweakerAutoTestingConfigBindings() is enabled.public void beforeTest()
public void afterTest()
public void afterClass()
BeforeAfterClassMethodsProviderThis will only be called if the beforeClass() method completed successfully, so you can be sure that the instanciation of the class is complete here.
afterClass in interface BeforeAfterClassMethodsProviderpublic void beforeClassException(Throwable ex)
BeforeAfterClassMethodsProviderBeforeAfterClassMethodsProvider.beforeClass() execution.
Be careful!! Here, you are pretty much certain that
the instanciation of the class was not succesful, so you
can't use any of its methods!beforeClassException in interface BeforeAfterClassMethodsProviderpublic void afterClassLoops()
RepeatedClassAfterMethodProviderThis will only be called if the beforeClass() method completed successfully, so you can be sure that the instanciation of the class is complete here.
afterClassLoops in interface RepeatedClassAfterMethodProviderpublic void testFailure(org.junit.runner.notification.Failure failure)
testFailure in interface TestFailureListener@Inject protected void setSpincastConfig(SpincastConfig spincastConfig)
protected SpincastConfig getSpincastConfig()
protected com.google.inject.Injector getInjector()
protected void deleteTempDir()
protected File getTestingWritableDir()
protected String createTestingFilePath(String relativePath)
protected String createTestingFilePath()
protected abstract com.google.inject.Injector createInjector()
main(...) method) or by
creating a custom Injector.Copyright © 2017. All rights reserved.