public class

MockComponentContainer

extends TestWatcher
java.lang.Object
   ↳ org.junit.rules.TestWatcher
     ↳ com.atlassian.jira.junit.rules.MockComponentContainer

Class Overview

JUnit @Rule that allows for providing mock JIRA components accessed in production code via ComponentAccessor static methods.

Example usage (in your test classes):

  • using annotations:
  •      @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.
         }
     
  • or adding to the container manually:
  •      @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.
         }
     

Summary

Public Constructors
MockComponentContainer(Object test)
Public Methods
<I, C extends I> MockComponentContainer addMock(Class<I> componentInterface, C mockComponentImplementation)
<I, C extends I> MockComponentContainer addMockComponent(Class<I> componentInterface, C mockComponentImplementation)
MockComponentWorker getMockWorker()
Protected Methods
void finished(Description description)
void starting(Description description)
[Expand]
Inherited Methods
From class org.junit.rules.TestWatcher
From class java.lang.Object
From interface org.junit.rules.TestRule

Public Constructors

public MockComponentContainer (Object test)

Public Methods

public MockComponentContainer addMock (Class<I> componentInterface, C mockComponentImplementation)

public MockComponentContainer addMockComponent (Class<I> componentInterface, C mockComponentImplementation)

public MockComponentWorker getMockWorker ()

Protected Methods

protected void finished (Description description)

protected void starting (Description description)