org.xwiki.test.mockito
Class MockitoComponentMockingRule<T>
java.lang.Object
org.xwiki.component.embed.EmbeddableComponentManager
org.xwiki.test.mockito.MockitoComponentMockingRule<T>
- Type Parameters:
T - the component role type, used to provide a typed instance when calling getComponentUnderTest()
- All Implemented Interfaces:
- org.junit.rules.MethodRule, org.xwiki.component.manager.ComponentManager, org.xwiki.component.phase.Disposable
public class MockitoComponentMockingRule<T>
- extends EmbeddableComponentManager
- implements org.junit.rules.MethodRule
Unit tests for Components should extend this class instead of using 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. This has 2 advantages:
- This is the spirit of this Rule since it's for unit testing and this testing your component in isolation from
the rest
- It makes the tests up to 10 times faster
If you really need to register some components, use the ComponentList annotation
and if you really really need to register all components (it takes time) then use
AllComponents.
- Since:
- 4.3.1
- Version:
- $Id: b6b427677e0d01d1c56f2b0f7a9aa60e952d64e7 $
|
Method Summary |
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runners.model.FrameworkMethod method,
Object target)
|
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()
|
| Methods inherited from class org.xwiki.component.embed.EmbeddableComponentManager |
dispose, getComponentDescriptor, getComponentDescriptorList, getComponentDescriptorList, getComponentEventManager, getComponentInstance, getInstance, getInstance, getInstanceList, getInstanceMap, getParent, hasComponent, hasComponent, initialize, registerComponent, registerComponent, release, setComponentEventManager, setParent, unregisterComponent, unregisterComponent |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
MockitoComponentMockingRule
public MockitoComponentMockingRule(Class<? extends T> componentImplementationClass)
- Parameters:
componentImplementationClass - the component implementation for which we wish to have its injection mocked
MockitoComponentMockingRule
public MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
List<? extends Class<?>> excludedComponentImplementationDependencies)
- Parameters:
componentImplementationClass - the component implementation for which we wish to have its injection mockedexcludedComponentImplementationDependencies - list of component dependencies class that we don't want mocked
MockitoComponentMockingRule
public MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
Type componentRoleType,
String componentRoleHint,
List<? extends Class<?>> excludedComponentImplementationDependencies)
- Parameters:
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 disambiguationexcludedComponentImplementationDependencies - list of component dependencies class that we don't want mocked
MockitoComponentMockingRule
public MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
Type componentRoleType,
List<? extends Class<?>> excludedComponentImplementationDependencies)
- Parameters:
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 disambiguationexcludedComponentImplementationDependencies - list of component dependencies class that we don't want mocked
MockitoComponentMockingRule
public MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
Type componentRoleType,
String componentRoleHint)
- Parameters:
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 disambiguation
MockitoComponentMockingRule
public MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
Type componentRoleType)
- Parameters:
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 disambiguation
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
org.junit.runners.model.FrameworkMethod method,
Object target)
- Specified by:
apply in interface org.junit.rules.MethodRule
createLogger
protected Object createLogger(Class<?> instanceClass)
- Overrides EmbeddableComponentManager in order to mock Loggers since they're handled specially and are not
components.
- Overrides:
createLogger in class EmbeddableComponentManager
- Parameters:
instanceClass - the injected class
- Returns:
- the logger
getComponentUnderTest
public T getComponentUnderTest()
throws org.xwiki.component.manager.ComponentLookupException
- Returns:
- the component which is having its injections being mocked by the
MockitoComponentMockingRule rule
- Throws:
org.xwiki.component.manager.ComponentLookupException - if the component under test has not been properly registered
getMockedLogger
public org.slf4j.Logger getMockedLogger()
- Returns:
- the mocked Logger if the Component under Test has requested an Injection of a Logger or null otherwise
Copyright © 2004-2013 XWiki. All Rights Reserved.