Add test for LoginCommand; create AuthMe mock test util

Had to create a getter for the Management instance in the AuthMe class for mocking, but fields should generally not be accessed globally. Hopefully soon we will be able to make the field private.
(cherry picked from commit f1a0022)
This commit is contained in:
ljacqu 2015-11-21 09:49:39 +01:00
parent 3934d67330
commit 987e38c5df
4 changed files with 113 additions and 1 deletions

View File

@ -1028,4 +1028,9 @@ public class AuthMe extends JavaPlugin {
public static int getVersionCode() { public static int getVersionCode() {
return PLUGIN_VERSION_CODE; return PLUGIN_VERSION_CODE;
} }
/** Returns the management instance. */
public Management getManagement() {
return management;
}
} }

View File

@ -24,7 +24,7 @@ public class LoginCommand extends ExecutableCommand {
final String playerPass = commandArguments.get(0); final String playerPass = commandArguments.get(0);
// Log the player in // Log the player in
plugin.management.performLogin(player, playerPass, false); plugin.getManagement().performLogin(player, playerPass, false);
return true; return true;
} }
} }

View File

@ -0,0 +1,30 @@
package fr.xephi.authme;
import org.mockito.Mockito;
import java.lang.reflect.Field;
/**
* Creates a mock implementation of AuthMe for testing purposes.
*/
public final class AuthMeMockUtil {
private AuthMeMockUtil() {
// Util class
}
/**
* Set the AuthMe plugin instance to a mock object. Use {@link AuthMe#getInstance()} to retrieve the mock.
*/
public static void initialize() {
AuthMe mock = Mockito.mock(AuthMe.class);
try {
Field instance = AuthMe.class.getDeclaredField("plugin");
instance.setAccessible(true);
instance.set(null, mock);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Could not initialize AuthMe mock", e);
}
}
}

View File

@ -0,0 +1,77 @@
package fr.xephi.authme.command.executable.login;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.Settings;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
/**
* Test for {@link LoginCommand}.
*/
public class LoginCommandTest {
private static Management managementMock;
@Before
public void initializeAuthMeMock() {
AuthMeMockUtil.initialize();
AuthMe pluginMock = AuthMe.getInstance();
Settings.captchaLength = 10;
managementMock = mock(Management.class);
Mockito.when(pluginMock.getManagement()).thenReturn(managementMock);
}
@Test
public void shouldStopIfSenderIsNotAPlayer() {
// given
CommandSender sender = mock(BlockCommandSender.class);
LoginCommand command = new LoginCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts());
// then
Mockito.verify(managementMock, never()).performLogin(any(Player.class), anyString(), anyBoolean());
}
@Test
public void shouldCallManagementForPlayerCaller() {
// given
Player sender = mock(Player.class);
LoginCommand command = new LoginCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("password"));
// then
Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false));
}
@Test
public void shouldHandleMissingPassword() {
// given
Player sender = mock(Player.class);
LoginCommand command = new LoginCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts());
// then
// TODO ljacqu 20151121: May make sense to handle null password in LoginCommand instead of forwarding the call
String password = null;
Mockito.verify(managementMock).performLogin(eq(sender), eq(password), eq(false));
}
}