mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-08-31 09:39:36 +02:00
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.
This commit is contained in:
parent
d81ef3168e
commit
4e8614fdf7
|
@ -952,4 +952,9 @@ public class AuthMe extends JavaPlugin {
|
|||
public static int getVersionCode() {
|
||||
return PLUGIN_VERSION_CODE;
|
||||
}
|
||||
|
||||
/** Returns the management instance. */
|
||||
public Management getManagement() {
|
||||
return management;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ public class LoginCommand extends ExecutableCommand {
|
|||
final String playerPass = commandArguments.get(0);
|
||||
|
||||
// Log the player in
|
||||
plugin.management.performLogin(player, playerPass, false);
|
||||
plugin.getManagement().performLogin(player, playerPass, false);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
30
src/test/java/fr/xephi/authme/AuthMeMockUtil.java
Normal file
30
src/test/java/fr/xephi/authme/AuthMeMockUtil.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user