Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

unit testing jira plugin using mocks

Joseph Kelroy July 11, 2018

Very new to Jira and mocking so any input is appreciated.

I am attempting to unit test a jira plugin but it seems like the set up environment is so complicated I just need to do very basic unit tests and save the rest for integration tests. 

My very simple plug in works exactly as intended after hours of dummy testing but I am trying to write formal tests.  This is from my TimeFunctions.java

public Long getIssuesInEpicTotalTime(Issue epic)
{
Long issuesTotalTime = 0L;
final String sLinkTypeName = "Epic-Story Link";
final Collection<IssueLink> links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(epic.getId());
for (final IssueLink link : links)
{
final String name = link.getIssueLinkType().getName();
final Issue destinationObject = link.getDestinationObject();
if (sLinkTypeName .equals(name))
{
try {
Long tempTime = destinationObject.getTimeSpent();
issuesTotalTime += tempTime;
} catch (Exception NullPointerException) {
// Do nothing if time is null
}
}
}
return issuesTotalTime;
}

Here is what I am trying to write a unit test for. 

Now in my unit test class the errors I get on the tests are things like component accessor not initialized. (on atlas-unit-test)

Here is the exact error: 

"This is not expected to occur on a production system.
Developers that encounter this message within a unit test
should use n MockitoMocksInContainer rule to initialize mockito and ComponentAccessor.
For more detailed explanation read the documentation
for MockComponentWorker in the jira-tests artifact for more information
about what causes this error and how to address it."

Something like this is what I want to impliment

IssueLink issueLink = mock(IssueLink.class,CALLS_REAL_METHODS);
Collection<IssueLink> issueLinks = new ArrayList<>();
ComponentAccessor ComponentAccessor = mock(ComponentAccessor.class, CALLS_REAL_METHODS);

@Test
public void getNullIssueTime() {
issueLinks.add(issueLink);
when(ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue1.getId())).thenReturn(issueLinks);
assertEquals((Long)0L,timeFunctions.getIssuesInEpicTotalTime(issue1));
}

From what I have tried I could not get jira-tests working as just a dependency but I do have it in <build><plugin>*my-maven-jira-plugin*<PluginArtifacts> in my main pom file. I want to use MockitoMocksInContainer ajd MockComponentWorker but I have read they have been deprecated from JIRA 7, which I am using. I am not sure if I should just mock everything like I am doing or I should use something built in. Or if I am going about it the complete wrong way let me know. The set up just seems to be so much for simple tests.

Thanks for any help!

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events