Junit for JiraRestClient create Issue

南笙酒味 提交于 2020-01-07 03:09:10

问题


I am trying to write a JUnit test for following method which creates a new Jira issue, do anyone know how to mock JiraRestClient or is there any other way to write a test for this

My code is

public Issue createNewIssue(BasicProject project, BasicUser assignee, BasicIssueType issueType, String summary, String description, String parentKey, File attachment)
 {
    try
    {
      IssueInputBuilder issueBuilder = new IssueInputBuilder(project, issueType);
      issueBuilder.setDescription(description);
      issueBuilder.setSummary(summary);
      issueBuilder.setProjectKey(project.getKey());
      issueBuilder.setIssueType(issueType);
      issueBuilder.setAssignee(assignee);
      if(parentKey != null)
      {
        Map<String, Object> parent = new HashMap<String, Object>();
        parent.put("key", parentKey);
        FieldInput parentField = new FieldInput("parent", new ComplexIssueInputFieldValue(parent));
        issueBuilder.setFieldInput(parentField);
      }
      IssueInput issueInput  = issueBuilder.build();

      IssueRestClient issueClient = getJiraRestClient().getIssueClient();
      BasicIssue newBasicIssue = issueClient.createIssue(issueInput, pm);
      Issue newIssue = issueClient.getIssue(newBasicIssue.getKey(), pm);
      if(attachment != null && newIssue != null)
        issueClient.addAttachments(pm, newIssue.getAttachmentsUri(), attachment); 
      return newIssue;
    } 
    catch (RestClientException e)
    {
      LOGGER.debug("Error while creating new Jira issue for input paramenters project : " + (project != null ? project.getName() : null) + " assignee : " +(assignee != null ? assignee.getName() : null)
          + " issueType : " + (issueType != null ? issueType.getName() : null) + " summary : " + summary + " description : " + description);
      return null;
    }
  }

UPDATE

One thing I am thinking of is to pass one parameter which will decide if method is running from test but then it will disturb the API which I don't want. But to write a test I must need to escape the call

BasicIssue newBasicIssue = issueClient.createIssue(issueInput, pm);

How to do that?


回答1:


In your code you've got a getJiraRestClient() method. If you have a setJiraRestClient(JiraRestClient client) method as well, you can call that one from your test class and set it as a mock client.

In Mockito you could initialize your mock client like this:

JiraRestClient mockClient = org.mockito.Mockito.mock(JiraRestClient.class);

That's assuming your Jira ReST client is in a class called JiraRestClient, of course. But then you can setup your mock to return a mocked IssueRestClient when its getIssueClient() is called, etc. So, like this:

IssueRestClient mockIssueClient = org.mockito.Mockito.mock(IssueRestClient.class);
org.mockito.Mockito.when(mockClient.getIssueClient()).thenReturn(mockIssueClient);

And so on.

If you don't have and don't want to create a setJiraRestClient method but you have the client as a field in your class, you could use the BeanInject library for injecting the mock bean into your class:

Inject.field("field").of(target).with("value");

If you're using Spring, you could create a different context file for your unit tests and have Spring inject a mock bean instead of the real JiraRestClient bean.




回答2:


OK. Since I've not used JIRA Open APIs directly. Instead I've used JIRA Client to automate JIRA related tasks.

In order to write unit tests for JIRA, you need to mock functions where you have implemented JIRA generic methods. For example, if you wish to get a list of attachments to a particular defect / ticket, your method would look like:

public class Generics {

private JiraClient jira;

    @Step("Get added attchments against issue <issueId>")
    public List<Attachment> getAttachments(String issueId) {
        List<Attachment> listOfAttachments = null;
        try {
            Issue issue = jira.getIssue(issueId);
            listOfAttachments = issue.getAttachments();
            LOGGER.info("Current Attachments to " + issueId + " " + listOfAttachments);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return listOfAttachments;
    }
}

Equivalent JUnit test:

public class JiraUnitTests {

    @Mock
    List<Attachment> attachments;

    @Test
    public void testGetAttachment() {
        Generics generics = Mockito.mock(Generics.class);
        Mockito.when(generics.getAttachments(Matchers.any(String.class))).thenReturn(attachments);
        Assert.assertEquals(attachments, generics.getAttachments(Matchers.any(String.class)));
    }

I have mocked below data members from Generics class:

  1. Variable attachments
  2. Method getAttachments()

Here, You don't really need to mock JIRA Client. Instead you could mock methods (if you have not modified core business logic / purpose of that method)



来源:https://stackoverflow.com/questions/27015121/junit-for-jirarestclient-create-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!