Class MockComponentContainer
- All Implemented Interfaces:
org.junit.rules.TestRule
@Rule
that allows for providing mock JIRA components accessed in
production code via ComponentAccessor
static methods.
Example usage (in your test classes):
- using annotations:
- or adding to the container manually:
@Rule public MockComponentContainer container = new MockComponentContainer(this);
@Mock
@AvailableInContainer
private ServiceOne mockServiceOne;
@Mock
@AvailableInContainer(interfaceClass=ServiceTwo.class) // useful if mockServiceTwo would implement more interfaces
private ServiceTwo mockServiceTwo;
@Test
public void testCodeThatUsesComponentAccessor()
{
assertSame(mockServiceOne, ComponentAccessor.getComponent(ServiceOne.class));
assertSame(mockServiceTwo, ComponentAccessor.getComponent(ServiceTwo.class));
// etc.
}
@Rule public MockComponentContainer container = new MockComponentContainer(this);
@Before
public void addMocks()
{
container.addMock(ServiceOne.class, mockServiceOne)
.addMock(ServiceTwo.class, mockServiceTwo);
}
@Test
public void testCodeThatUsesComponentAccessor()
{
assertSame(mockServiceOne, ComponentAccessor.getComponent(ServiceOne.class));
// etc.
}
- Since:
- 5.1
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<I,
C extends I>
MockComponentContainerBase<I,
C extends I>
MockComponentContainerBaseaddMockComponent
(Class<I> componentInterface, C mockComponentImplementation) protected void
finished
(org.junit.runner.Description description) protected void
starting
(org.junit.runner.Description description) Methods inherited from class org.junit.rules.TestWatcher
apply, failed, skipped, skipped, succeeded
-
Constructor Details
-
MockComponentContainer
-
-
Method Details
-
addMockComponent
public <I,C extends I> MockComponentContainerBase addMockComponent(Class<I> componentInterface, C mockComponentImplementation) -
addMock
public <I,C extends I> MockComponentContainerBase addMock(Class<I> componentInterface, C mockComponentImplementation) -
starting
protected void starting(org.junit.runner.Description description) - Overrides:
starting
in classorg.junit.rules.TestWatcher
-
finished
protected void finished(org.junit.runner.Description description) - Overrides:
finished
in classorg.junit.rules.TestWatcher
-
getMockWorker
-