|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.atlassian.jira.mock.component.MockComponentWorker
public class MockComponentWorker
This component worker can be used with the ComponentAccessor
to return mock
instances of components for unit testing.
ComponentAccessor.getComponentOfType(Class)
to resolve the dependency, instead. The drawback is that ComponentAccessor
uses a global, static reference to a ComponentAccessor.Worker
implementation
to accomplish this, and if nothing has initialised that reference, then an
IllegalStateException
is thrown.
Unit tests must be responsible for ensuring that everything they require, directly or
indirectly, is arranged during the test's setup, so a unit test that encounters this
problem should explicitly initialise the component accessor. In most cases, all they
need to do is create and install an instance of this class. For example:
@Before
public void setUp()
{
new MockComponentWorker()
.init()
;
}
The MockComponentWorker
comes with a few mocks by default, including
implementations for common problem areas, such as the UserKeyService
and
the UserPreferencesManager
, so for many tests this will be enough as-is.
If you need additional mocked components to be resolved in this way, then you can
add them as well. An example might look something like this:
@Before
public void setUp()
{
final ApplicationUser fred = new MockApplicationUser("Fred");
final JiraAuthenticationContext jiraAuthenticationContext = Mockito.mock(JiraAuthenticationContext.class);
Mockito.when(jiraAuthenticationContext.getUser()).thenReturn(fred);
Mockito.when(jiraAuthenticationContext.getLoggedInUser()).thenReturn(fred.getDirectoryUser());
new MockComponentWorker()
.addMock
(ConstantsManager.class, new MockStatusConstantsManager())
.addMock
(JiraAuthenticationContext.class, jiraAuthenticationContext)
.init()
;
}
JUnit 4 annotations can also be used to initialise the MockComponentWorker
.
See the
MockComponentContainer
and
MockitoContainer
@Rule
s for examples.
Constructor Summary | |
---|---|
MockComponentWorker()
|
Method Summary | ||
---|---|---|
|
addMock(Class<T> componentInterface,
U componentMock)
Registers a mock component to be returned by this component worker. |
|
|
getComponent(Class<T> componentClass)
Obtains the registered mock of the specified interface type. |
|
|
getComponentOfType(Class<T> componentClass)
Although the ComponentAccessor specifies different semantics for this method,
in this mock implementation it behaves identically to getComponent(Class) . |
|
MockApplicationProperties |
getMockApplicationProperties()
|
|
MockUserKeyStore |
getMockUserKeyStore()
|
|
MockUserPreferencesManager |
getMockUserPreferences()
|
|
|
getOSGiComponentInstanceOfType(Class<T> componentClass)
Although the ComponentAccessor specifies different semantics for this method,
in this mock implementation it behaves identically to getComponent(Class) . |
|
UserKeyServiceImpl |
getUserKeyService()
|
|
MockComponentWorker |
init()
Convenience method that just calls ComponentAccessor .initialiseWorker(this) . |
|
|
registerMock(Class<T> componentInterface,
U componentMock)
Registers a mock component to be returned by this component worker. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public MockComponentWorker()
Method Detail |
---|
public <T,U extends T> void registerMock(Class<T> componentInterface, U componentMock)
addMock(Class, Object)
is identical but also returns this
for call chaining, it may be more convenient to use.
T
- the componentInterface
U
- the componentMock
's class, which must implement <T>
componentInterface
- the interface that the mock component must implementcomponentMock
- the component that implements the interfaceaddMock(Class, Object)
public <T,U extends T> MockComponentWorker addMock(Class<T> componentInterface, U componentMock)
registerMock(Class, Object)
, except that
it also returns this
, making it possible to use it in the convenient call chaining
style illustrated in the documentation for this class.
T
- the componentInterface
U
- the componentMock
's class, which must implement <T>
componentInterface
- the interface that the mock component must implementcomponentMock
- the component that implements the interface
this
, for conveniencepublic <T> T getComponent(Class<T> componentClass)
UserKeyService
) or that the test registered earlier and is now ready to stub.
JIRA itself will also sometimes use this method (or getComponentOfType(Class)
) to
resolve dependencies. Arguably, this method should throw an exception rather than return
null
when a component is requested without a mock provided for it; however, some
unit tests will resolve the component during construction without the test code path
actually needing to use it, so this method just generates a warning message in the log
for this case.
getComponent
in interface ComponentAccessor.Worker
T
- the return type inferred from the specified componentClass
.componentClass
- the interface for which an implementation is desired
null
if no mock implementation has been
provided for it.public <T> T getComponentOfType(Class<T> componentClass)
ComponentAccessor
specifies different semantics for this method,
in this mock implementation it behaves identically to getComponent(Class)
.
getComponentOfType
in interface ComponentAccessor.Worker
T
- as for getComponent(Class)
componentClass
- as for getComponent(Class)
getComponent(Class)
public <T> T getOSGiComponentInstanceOfType(Class<T> componentClass)
ComponentAccessor
specifies different semantics for this method,
in this mock implementation it behaves identically to getComponent(Class)
.
getOSGiComponentInstanceOfType
in interface ComponentAccessor.Worker
T
- as for getComponent(Class)
componentClass
- as for getComponent(Class)
getComponent(Class)
public MockComponentWorker init()
ComponentAccessor
.initialiseWorker(this)
.
If you are developing a plugin that must support JIRA versions prior to v6.0, then that @Internal
method must be used directly, instead.
public MockUserKeyStore getMockUserKeyStore()
public MockUserPreferencesManager getMockUserPreferences()
public MockApplicationProperties getMockApplicationProperties()
public UserKeyServiceImpl getUserKeyService()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |