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
Community moderators have prevented the ability to post new answers.
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.
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")); } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great! Mockito is really nice - we use it for Confluence's own unit tests, too. :-)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.