It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

[SOLVED] How to use dependency injection for unit tests?

R K Feb 25, 2013

Could you suggest how to conduct dependency injection for unit tests?

Here's my code so far:

package it.de.r_k.atlas;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.junit.Test;
import org.junit.Before;
import org.junit.After;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import de.r_k.atlas.MyPluginComponent;
import de.r_k.atlas.MyPluginComponentImpl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.runner.RunWith;
import com.atlassian.plugins.osgi.test.AtlassianPluginsTestRunner;
import de.r_k.atlas.UserResource;

import com.atlassian.confluence.user.UserAccessor;
import com.atlassian.user.impl.DefaultUser;
import com.atlassian.user.security.password.Credential;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;

@RunWith(AtlassianPluginsTestRunner.class)
public class UserResourceTest
{
	private String name;
	private String pw;
	private List<String> groups = new ArrayList<String>();
	
    private UserAccessor userAccessor;
    public void setUserAccessor(UserAccessor userAccessor)
    {
        this.userAccessor = userAccessor;
    }

	@Before
	public void initialize() {
    	AuthenticatedUserThreadLocal.setUser(new DefaultUser("admin"));
    	
    	Random rng = new Random();
    	String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    	char[] text = new char[10];
		for (int i = 0; i < 10; i++)
        {
            text[i] = characters.charAt(rng.nextInt(characters.length()));
        }
    	
		this.name = "testuser_" + text;
		this.pw = "test";
		this.groups.add("test-germany");
		int r_kint = userAccessor.countUsersWithConfluenceAccess();
    }
}

it fails on the usage of userAccessor with java.lang.NullPointerException

1 answer

1 accepted

1 vote
Answer accepted
Joe Clark Atlassian Team Feb 26, 2013

It doesn't look like you're writing a unit test - it looks like you're writing a Wired Plugin Test using the Plugin SDK. You need to run these tests within the Confluence server process using the test runner console - you cannot execute these tests using an IDE or from the command line, for example.

Here's our documentation, which provides more information on how run these kinds of tests: https://developer.atlassian.com/display/DOCS/Run+Wired+Tests+with+the+Plugin+Test+Console

If you run the test within Confluence, then the dependency injection should work automatically.

R K Feb 26, 2013

Thanks Joseph. I haven't tried your solution but instead changed my code so that is a unit test. I'm importing Mockito to mock some classes and objects that otherwise should be provided by Confluence. Here's my code for future reference of others running into the same issue:

package it.de.rktest.atlas;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.junit.Test;
import org.junit.Before;
import org.junit.After;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import de.innogames.atlas.MyPluginComponent;
import de.innogames.atlas.MyPluginComponentImpl;

import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import com.atlassian.plugins.osgi.test.AtlassianPluginsTestRunner;
import de.innogames.atlas.UserResource;

import com.atlassian.confluence.user.UserAccessor;
import com.atlassian.user.impl.DefaultUser;
import com.atlassian.user.security.password.Credential;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;

import static org.mockito.Mockito.*;

@RunWith(AtlassianPluginsTestRunner.class)
public class UserResourceTest
{

	private UserResource userResource = new UserResource();
	
	@Before
	public void initialize() {
		UserAccessor mockedUserAccessor = mock(UserAccessor.class);
		when(mockedUserAccessor.getUser("existingMockTestUser")).thenReturn(new DefaultUser("justGiveAnyNameHere"));
		when(mockedUserAccessor.getUser("missingMockTestUser")).thenReturn(null);
	}
	
	@After
	public void tearDown() {

	}
	
	
    @Test
    public void userExistsWithExistingUser()
    {
    	assertTrue(userResource.userExists("existingMockTestUser"));
    }
   }

R K Feb 26, 2013

For those wishing to use Mockito like me, here's how to include it's dependency: http://code.google.com/p/mockito/wiki/DeclaringMockitoDependency

add that as a new dependency to your pom.xml

Good luck!

Joe Clark Atlassian Team Feb 27, 2013

Great! Mockito is really nice - we use it for Confluence's own unit tests, too. :-)

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

8,151 views 25 36
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you