问题描述
我正在尝试为以下方法编写JUnit测试,这会产生一个新的Jira问题,是否有人知道如何模拟JiraRestClient
或是否有其他方法为此编写测试
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
我的代码是
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;
}
}
更新
我想到的一件事是传递一个参数,该参数将确定方法是否正在测试中运行,但是这会干扰我不想要的API.但是要编写测试,我必须逃脱呼叫
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);
该怎么做?
推荐答案
确定.由于我没有直接使用JIRA Open API.相反,我使用了 JIRA客户端来自动执行与JIRA相关的任务.
OK. Since I've not used JIRA Open APIs directly. Instead I've used JIRA Client to automate JIRA related tasks.
为了编写JIRA的单元测试,您需要在实现JIRA通用方法的地方模拟函数.例如,如果您希望获得特定缺陷/故障单的附件列表,则您的方法应类似于:
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;
}
}
等效的JUnit测试:
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:
- 各种附件
- 方法getAttachments()
在这里,您实际上并不需要模拟JIRA Client.相反,您可以模拟方法(如果您尚未修改核心业务逻辑/该方法的目的)
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)
这篇关于Junit for JiraRestClient创建问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!