A Plugin for Atlassian Jira – Unit Tests

12 December 2000
| |
Reading time: 3 minutes

Previous Page | Next Page | Pages: 0 1 2 3 4 5 6 7

Automated testing

So far, we have only used the very rudimentary unit and integration tests generated by the Atlassian tools. In order to perform proper test-driven development (TDD), these need to be extended. In general, there should be a thorough unit test of each custom Java module and a simpler integration test of the same, which tests the “happy day” scenarios as well as one or two of the principal failure modes.

Of course the MetricsInfoImpl class was developed using TDD, although I presented the finished code above as a fait accompli.

Unit Tests

All unit tests are located in the src/test/java source folder under the package ut.path.to.your.package. In this example, the package name is ut.com.zuhlke.training.jira (generated automatically by the initial atlas-create-jira-plugin command). These tests run both under the IDE and under Maven whenever the software is built. I will present one happy-day test case to show how the whole thing works.

The unit tests have to emulate the context in which the plugin will run. This makes the setup quite verbose. Jira uses the mockito framework to make it easy to mock the environment – you just need to know what parts of the environment to mock!

Start by creating the Java class MetricsInfoImplUnitTest and writing a failing test:

package ut.com.zuhlke.training.jira;

import com.zuhlke.training.jira.api.MetricsInfo;
import com.zuhlke.training.jira.impl.MetricsInfoImpl;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertNotNull;

public class MetricsInfoImplUnitTest {

    public void shouldRetrieveContextMapForProject() {

        final MetricsInfo metricsInfo = new MetricsInfoImpl(userProjectHistoryManager);
        final Map<String, Object> contextMap = metricsInfo.getContextMap(applicationUser, jiraHelper);

        assertNotNull("contextMap should not be null", contextMap);

This fails to compile, because the objects userProjectHistoryManager, applicationUser and jiraHelper are undefined. Add some mocks to satisfy these requirements (above the first test case):

public MockitoContainer mockitoContainer = MockitoMocksInContainer.rule(this);

private UserProjectHistoryManager userProjectHistoryManager;

private ApplicationUser applicationUser;

private JiraHelper jiraHelper;

The IDE automatically adds the necessary imports:

import com.atlassian.jira.junit.rules.MockitoContainer;
import com.atlassian.jira.junit.rules.MockitoMocksInContainer;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.UserProjectHistoryManager;
import org.junit.Rule;
import org.mockito.Mock;

Run the test to prove that it fails. Surprisingly perhaps, it passes – because of the defensive code in the implementation class, which I inserted following the failure of this test. So let’s add another assertion:

assertEquals("Project", project, contextMap.get(PROJECT));

This adds another import and requires us to add another mock object. This one is provided in quite a different manner. Just above the test case, add the following:

final String projectKey            = "SAM";
final String projectName           = "SampleProject";
final MockProject project          = new MockProject(10001L, projectKey, projectName);

The following imports are added automatically (you may have to respond to the IDE’s prompt with ALT-ENTER):

import static com.zuhlke.training.jira.api.MetricsInfo.PROJECT;
import com.atlassian.jira.project.MockProject;
import static org.junit.Assert.assertEquals;

Rather satisfyingly, the test now runs and fails. The context map has returned null when the object with the key PROJECT was requested. So a little more initialisation is needed at the start of the test case:

Mockito.when(userProjectHistoryManager.getCurrentProject(PROJECT_ADMIN, applicationUser)).thenReturn(project);

This adds another import:

import static com.atlassian.jira.security.Permissions.PROJECT_ADMIN;

The test now passes. Just to be sure that we can access properties of the returned object, add the following to the end of the test case:

final Project contextProject = (Project) contextMap.get(PROJECT);
assertEquals("Project Name", project.getName(), contextProject.getName());

The IDE adds the import:

import com.atlassian.jira.project.Project;

Previous Page | Next Page | Pages: 0 1 2 3 4 5 6 7

Comments (0)


Sign up for our Updates

Sign up now for our updates.

This field is required
This field is required
This field is required

I'm interested in:

Select at least one category
You were signed up successfully.

Receive regular updates from our blog


Or would you like to discuss a potential project with us? Contact us »