T
- the component role type, used to provide a typed instance when calling getComponentUnderTest()
public class MockitoComponentMockingRule<T> extends MockitoComponentManagerRule
MockitoComponentManagerRule
or
ComponentManagerRule
which should only be used for integration tests.
To use this class, define a JUnit @Rule
and pass the component implementation class that you wish to have
mocked for you. Then in your test code, do a lookup of your component under test and you'll get a component instance
which has all its injected dependencies mocked automatically.
For example:
public class MyComponentTest
{
@Rule
public final MockitoComponentMockingRule<MyComponent> mocker =
new MockitoComponentMockingRule(MyImplementation.class);
@Test
public void someTest() throws Exception
{
MyComponent myComponent = mocker.getComponentUnderTest();
...
}
...
}
Note that by default there are no component registered against the component manager except those mocked
automatically by the Rule (except for the MockitoComponentMockingRule itself, which means that if your component
under test is injected a default ComponentManager, it'll be the MockitoComponentMockingRule which will get injected.
See more below). This has 2 advantages:
ComponentList
annotation
and if you really really need to register all components (it takes time) then use
AllComponents
.
In addition, you can perform some action before any component is registered in the Component Manager by having one or
several methods annotated with BeforeComponent
. Similarly, you can perform an
action after all components have been registered in the Component Manager by having one or several methods annotated
with AfterComponent
.
This can be useful (for example) in the case you wish to register a mock ComponentManager in your component under test. You would write:
@Rule
public final MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
@AfterComponent
public void overrideComponents() throws Exception
{
this.mocker.registerMockComponent(ComponentManager.class);
}
componentRegistrator
Constructor and Description |
---|
MockitoComponentMockingRule(Class<? extends T> componentImplementationClass) |
MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
List<? extends Class<?>> excludedComponentRoleDependencies) |
MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
Type componentRoleType) |
MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
Type componentRoleType,
List<? extends Class<?>> excludedComponentRoleDependencies) |
MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
Type componentRoleType,
String componentRoleHint) |
MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
Type componentRoleType,
String componentRoleHint,
List<? extends Class<?>> excludedComponentRoleDependencies) |
Modifier and Type | Method and Description |
---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runners.model.FrameworkMethod method,
Object target) |
protected void |
before(org.junit.runners.model.Statement base,
org.junit.runners.model.FrameworkMethod method,
Object target)
Called before the test.
|
protected Object |
createLogger(Class<?> instanceClass)
Overrides EmbeddableComponentManager in order to mock Loggers since they're handled specially and are not
components.
|
T |
getComponentUnderTest() |
org.slf4j.Logger |
getMockedLogger() |
after
registerMockComponent, registerMockComponent, registerMockComponent, registerMockComponent
initializeTest, notifyComponentDescriptorEvent, registerComponent, registerComponent, registerComponent, registerComponentIfDontExist, registerMemoryConfigurationSource, shutdownTest
createGenericProvider, dispose, getComponentDescriptor, getComponentDescriptorList, getComponentDescriptorList, getComponentEventManager, getComponentInstance, getDependencyInstance, getInstance, getInstance, getInstanceList, getInstanceMap, getNamespace, getParent, hasComponent, hasComponent, initialize, registerComponent, registerComponent, release, setComponentEventManager, setParent, unregisterComponent, unregisterComponent
public MockitoComponentMockingRule(Class<? extends T> componentImplementationClass)
componentImplementationClass
- the component implementation for which we wish to have its injection mockedpublic MockitoComponentMockingRule(Class<? extends T> componentImplementationClass, List<? extends Class<?>> excludedComponentRoleDependencies)
componentImplementationClass
- the component implementation for which we wish to have its injection mockedexcludedComponentRoleDependencies
- list of component dependency role classes that we don't want mockedpublic MockitoComponentMockingRule(Class<? extends T> componentImplementationClass, Type componentRoleType, String componentRoleHint, List<? extends Class<?>> excludedComponentRoleDependencies)
componentImplementationClass
- the component implementation for which we wish to have its injection mockedcomponentRoleType
- the role type of the component implementation (when it has several), for disambiguationcomponentRoleHint
- the role hint of the component implementation (when it has several), for disambiguationexcludedComponentRoleDependencies
- list of component dependency role classes that we don't want mockedpublic MockitoComponentMockingRule(Class<? extends T> componentImplementationClass, Type componentRoleType, List<? extends Class<?>> excludedComponentRoleDependencies)
componentImplementationClass
- the component implementation for which we wish to have its injection mockedcomponentRoleType
- the role type of the component implementation (when it has several), for disambiguationexcludedComponentRoleDependencies
- list of component dependency role classes that we don't want mockedpublic MockitoComponentMockingRule(Class<? extends T> componentImplementationClass, Type componentRoleType, String componentRoleHint)
componentImplementationClass
- the component implementation for which we wish to have its injection mockedcomponentRoleType
- the role type of the component implementation (when it has several), for disambiguationcomponentRoleHint
- the role hint of the component implementation (when it has several), for disambiguationpublic MockitoComponentMockingRule(Class<? extends T> componentImplementationClass, Type componentRoleType)
componentImplementationClass
- the component implementation for which we wish to have its injection mockedcomponentRoleType
- the role type of the component implementation (when it has several), for disambiguationpublic org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runners.model.FrameworkMethod method, Object target)
apply
in interface org.junit.rules.MethodRule
apply
in class MockitoComponentManagerRule
protected void before(org.junit.runners.model.Statement base, org.junit.runners.model.FrameworkMethod method, Object target) throws Throwable
before
in class MockitoComponentManagerRule
base
- The Statement
to be modifiedmethod
- The method to be runtarget
- The object on with the method will be run.Throwable
- if anything goes wrongprotected Object createLogger(Class<?> instanceClass)
createLogger
in class EmbeddableComponentManager
instanceClass
- the injected classpublic T getComponentUnderTest() throws org.xwiki.component.manager.ComponentLookupException
MockitoComponentMockingRule
ruleorg.xwiki.component.manager.ComponentLookupException
- if the component under test has not been properly registeredpublic org.slf4j.Logger getMockedLogger()
Copyright © 2004–2019 XWiki. All rights reserved.