In this post, We’ll learn how to mock JIRA API client to fake all requests to JIRA server using mocking framework, Mockito for our unit tests.
Add JIRA Client dependency, Mockito dependency and JUnit dependency to your project by appending pom.xml (MAVEN)
Step 1: Mock JIRA Client
JiraClient mockJiraClientRefObj = Mockito.mock(JiraClient.class);
We’ll be calling all jira in-built functions using mocked reference object (mockJiraClientRefObj) as we have now mocked JiraClient class.
Step 2: Apply mocking condition
Mockito.when(jiraClient.getIssue(Matchers.any(String.class))).thenReturn(issue);
Here, We’ve asked getIssue() function to return issue of type Issue.
Step 3: Apply Test Assertion
Assert.assertEquals(jiraClient.getIssue(Matchers.any(String.class)), issue);
Most important step while writing unit tests is to incorporate assert statements.
Step 4: Wrap it around JUnit annotation
@Test public void testGetIssue() throws JiraException {
JiraClient mockJiraClientRefObj = Mockito.mock(JiraClient.class); Mockito.when(jiraClient.getIssue(Matchers.any(String.class))).thenReturn(issue); Assert.assertEquals(jiraClient.getIssue(Matchers.any(String.class)), issue);
}
Click here to see full example.