mockito - Junit for JiraRestClient create Issue -


i trying write junit test following method creates new jira issue, know how mock jirarestclient or there other way write test this

my code

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 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 thinking of pass 1 parameter decide if method running test disturb api don't want. write test must need escape call

basicissue newbasicissue = issueclient.createissue(issueinput, pm);

how that?

in code you've got getjirarestclient() method. if have setjirarestclient(jirarestclient client) method well, can call 1 test class , set mock client.

in mockito initialize mock client this:

jirarestclient mockclient = org.mockito.mockito.mock(jirarestclient.class); 

that's assuming jira rest client in class called jirarestclient, of course. can setup mock return mocked issuerestclient when getissueclient() called, etc. so, this:

issuerestclient mockissueclient = org.mockito.mockito.mock(issuerestclient.class); org.mockito.mockito.when(mockclient.getissueclient()).thenreturn(mockissueclient); 

and on.

if don't have , don't want create setjirarestclient method have client field in class, use beaninject library injecting mock bean class:

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

if you're using spring, create different context file unit tests , have spring inject mock bean instead of real jirarestclient bean.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -