org.xwiki.test.mockito
Class MockitoComponentMockingRule<T>

java.lang.Object
  extended by org.xwiki.component.embed.EmbeddableComponentManager
      extended by org.xwiki.test.TestComponentManager
          extended by org.xwiki.test.ComponentManagerRule
              extended by org.xwiki.test.mockito.MockitoComponentManagerRule
                  extended by 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 MockitoComponentManagerRule

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:

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: 6f4a74afc398e344bc27c097c0c52dab6f562c64 $

Field Summary
 
Fields inherited from class org.xwiki.test.TestComponentManager
componentRegistrator
 
Constructor Summary
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)
           
 
Method Summary
 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()
           
 
Methods inherited from class org.xwiki.test.mockito.MockitoComponentManagerRule
registerMockComponent, registerMockComponent
 
Methods inherited from class org.xwiki.test.ComponentManagerRule
after
 
Methods inherited from class org.xwiki.test.TestComponentManager
initializeTest, registerComponent, registerComponent, registerComponent, registerMemoryConfigurationSource, shutdownTest
 
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
 

Constructor Detail

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<?>> excludedComponentRoleDependencies)
Parameters:
componentImplementationClass - the component implementation for which we wish to have its injection mocked
excludedComponentRoleDependencies - list of component dependency role classes that we don't want mocked

MockitoComponentMockingRule

public MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
                                   Type componentRoleType,
                                   String componentRoleHint,
                                   List<? extends Class<?>> excludedComponentRoleDependencies)
Parameters:
componentImplementationClass - the component implementation for which we wish to have its injection mocked
componentRoleType - the role type of the component implementation (when it has several), for disambiguation
componentRoleHint - the role hint of the component implementation (when it has several), for disambiguation
excludedComponentRoleDependencies - list of component dependency role classes that we don't want mocked

MockitoComponentMockingRule

public MockitoComponentMockingRule(Class<? extends T> componentImplementationClass,
                                   Type componentRoleType,
                                   List<? extends Class<?>> excludedComponentRoleDependencies)
Parameters:
componentImplementationClass - the component implementation for which we wish to have its injection mocked
componentRoleType - the role type of the component implementation (when it has several), for disambiguation
excludedComponentRoleDependencies - list of component dependency role classes 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 mocked
componentRoleType - the role type of the component implementation (when it has several), for disambiguation
componentRoleHint - 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 mocked
componentRoleType - the role type of the component implementation (when it has several), for disambiguation
Method Detail

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
Overrides:
apply in class ComponentManagerRule

before

protected void before(org.junit.runners.model.Statement base,
                      org.junit.runners.model.FrameworkMethod method,
                      Object target)
               throws Throwable
Called before the test.

Overrides:
before in class ComponentManagerRule
Parameters:
base - The Statement to be modified
method - The method to be run
target - The object on with the method will be run.
Throws:
Throwable - if anything goes wrong
Since:
5.1M1

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–2014 XWiki. All rights reserved.