public class MockitoContainer extends Object implements org.junit.rules.TestRule
MockComponentContainer. This will take care of initializing mocks using
Mockito and also putting them in a mock component container so that they're available via
ComponentAccessor.getComponent(Class). After the test has run, the mock
container will be tear down so that next test does not accidentally access stale state.
Usage:
@Rule public MockitoContainer mockitoContainer = new MockitoContainer(this);
@Mock
@AvailableInContainer
private UserService mockUserService;
@Mock private JiraAuthenticationContext jiraAuthenticationContext;
where the userService will be available via ComponentAccessor, but the
jiraAuthenticationContext will just be instantiated as a Mockito mock without adding it to the container.
The rule also gives the ability to access the initialized MockComponentWorker, which allows you to
manipulate the mocks directly from tests or add additional mocks that are only needed by some of the tests
or otherwise could not be made available using the @AvailableInContainer annotation.
Example:
@Rule public MockitoContainer mockitoContainer = new MockitoContainer(this);
@Mock
public void testSomething() {
mockitoContainer.getMockWorker().getMockUserKeyService().setMapping("fredkey", "FredName");
mockitoContainer.getMockWorker().addMock(ComponentInterface.class, new MockComponentImpl());
(...)
}
For JUnit5 use MockComponentContainerExtension instead.| Constructor and Description |
|---|
MockitoContainer(Object test) |
| Modifier and Type | Method and Description |
|---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description) |
org.junit.rules.RuleChain |
getInnerChain() |
MockComponentContainer |
getMockComponentContainer() |
MockComponentWorker |
getMockWorker()
A shorthand for getMockComponentContainer().getMockWorker()
|
public MockitoContainer(Object test)
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
apply in interface org.junit.rules.TestRulepublic MockComponentContainer getMockComponentContainer()
public MockComponentWorker getMockWorker()
public org.junit.rules.RuleChain getInnerChain()
Copyright © 2002-2024 Atlassian. All Rights Reserved.