1

In production the com.atlassian.jira.bc.issue.IssueService is injected in construction but how to mock it in unit tests?

production code:

class myObject {

    IssueService issueService    

    public myObject( IssueService issueService, ...) {
        this.issueService = issueService;
    }

    public void foo () {
    ...
    IssueInputParameters iip = issueService.newIssueInputParameters(); // NULL POINTER EXCEPTION WHEN RUN BU UNIT TEST!
    ...
    }
}

I have tried to write in test:

IssueService issueService = ComponentAccessor.getIssueService();
MyObject mo = new MyObject(issueService, ...);
mo.foo(); // NULL POINTER EXCEPTION!

The ComponentAccessor.getIssueService(); returns null instead of an object.

wykopowiedz
  • 71
  • 2
  • 12

1 Answers1

0

When initiating test cases, most Jira Services become unreachable for scoping purposes and it is advised to use a mocking version, which you can do as follows

import static org.mockito.Mockito.mock;
import org.junit.Before;
import org.junit.Test;

public class FooUnitTest {


    private IssueService   mockIssueService;
    private MyObject mo;

    @Before
    public void setup() {

        mockIssueService = mock(IssueService.class);

        mo = new MyObject(mockIssueService , ...);

    }

    @Test
    public void testFoo(){
        mo.foo(); 
    }
}
AbdulKarim
  • 605
  • 5
  • 18