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.
A GuiceTweaker instance is used to
be able to tweak a Guice context automagically. This for example
allows you to start your actual application, using its main() method.
Note that the Guice tweaker only works when the Guice context is created
using the standard Spincast Bootstrapper .
Note that you can annotate a test class with:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)if you need its tests to be ran in order (using the alphabetical order of the test methods' names).
| Modifier and Type | Field and Description |
|---|---|
protected static org.slf4j.Logger |
logger |
protected SpincastConfig |
spincastConfig |
| 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()
This method will be called after each test.
|
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()
This method will be called before each test.
|
protected GuiceTweaker |
createGuiceTweaker() |
protected abstract com.google.inject.Injector |
createInjector()
The test class must implement this method to create
the Guice injector.
|
protected File |
createTestingDir()
Create a temp directory.
|
protected String |
createTestingFilePath()
Returns a unique path to use to create a temporary test file.
|
protected String |
createTestingFilePath(String relativePath)
Returns the absolute path to use, given the relative one,
to create a temporary test file.
|
protected void |
deleteTestingWritableTempDir()
Deletes the testing writable temp directory.
|
protected Set<com.google.inject.Key<?>> |
getExtraExactBindingsToRemoveBeforePlugins()
Extra exact bindings to remvoe before the
plugins are applied.
|
protected Map<String,String> |
getExtraSystemProperties()
Extra System properties to set before the tests are run.
|
protected com.google.inject.Module |
getGuiceTweakerExtraOverridingModule()
If an overriding Module is to be added using the
Guice tweaker.
|
protected List<SpincastPlugin> |
getGuiceTweakerExtraPlugins()
Extra plugins to be added by the Guice Tweaker.
|
protected GuiceTweaker |
getGuiceTweakerFromThreadLocal() |
protected Set<String> |
getGuiceTweakerPluginsToDisable()
Ids of plugins to disable.
|
protected com.google.inject.Injector |
getInjector()
Returns the Guice injector.
|
protected SpincastConfig |
getSpincastConfig() |
protected Class<? extends SpincastConfig> |
getTestingConfigImplementationClass()
The implementation to use for the
SpincastConfig binding,
when running tests. |
protected File |
getTestingWritableTempDir()
Returns the directory that can be used to create files
and subdirectories during testing.
|
protected boolean |
isDisableBindCurrentClass() |
protected void |
resetSystemProperties()
Resets System properties.
|
void |
testFailure(org.junit.runner.notification.Failure failure)
You can override this method to be
informed when a test fails.
|
protected void |
tweakConfigurations(GuiceTweaker guiceTweaker) |
protected void |
validateCreatedInjector(com.google.inject.Injector guice)
Validates the created Injector, before the
dependencies are injected in the test class.
|
protected static final org.slf4j.Logger logger
@Inject protected SpincastConfig spincastConfig
public void beforeClass()
BeforeAfterClassMethodsProviderbeforeClass in interface BeforeAfterClassMethodsProviderprotected void addExtraSystemProperties()
getExtraSystemProperties() method.protected Map<String,String> getExtraSystemProperties()
protected Set<com.google.inject.Key<?>> getExtraExactBindingsToRemoveBeforePlugins()
protected void resetSystemProperties()
protected GuiceTweaker getGuiceTweakerFromThreadLocal()
protected void validateCreatedInjector(com.google.inject.Injector guice)
protected GuiceTweaker createGuiceTweaker()
protected Set<String> getGuiceTweakerPluginsToDisable()
Example:
SetpluginIdsToIgnore = super.getGuiceTweakerPluginsToDisable(); pluginIdsToIgnore.add(XXXXXX); return pluginIdsToIgnore;
protected void tweakConfigurations(GuiceTweaker guiceTweaker)
protected boolean isDisableBindCurrentClass()
protected List<SpincastPlugin> getGuiceTweakerExtraPlugins()
protected com.google.inject.Module getGuiceTweakerExtraOverridingModule()
Can be overriden with something like :
return Modules.override(super.getGuiceTweakerExtraOverridingModule()).with(new SpincastGuiceModuleBase() {
protected void configure() {
// ...
}
});
protected Class<? extends SpincastConfig> getTestingConfigImplementationClass()
SpincastConfig binding,
when running tests.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 TestFailureListenerprotected com.google.inject.Injector getInjector()
protected void deleteTestingWritableTempDir()
protected File getTestingWritableTempDir()
This directory will be deleted when the tests are done.
protected SpincastConfig getSpincastConfig()
protected String createTestingFilePath(String relativePath)
This file will be deleted when the tests are done.
protected String createTestingFilePath()
This file will be deleted when the tests are done.
protected File createTestingDir()
This directory will be deleted when the tests are done.
protected abstract com.google.inject.Injector createInjector()
main(...) method) or by
creating a custom Injector.Copyright © 2019. All rights reserved.