Minor - code householding (tests)

- Remove redundant uses of WrapperMock
- Use assertThat() from JUnit, not hamcrest
- Use hamcrest Matchers everywhere (not BaseMatchers etc.)
- Favor Mockito's argThat() over using ArgumentCaptor (more succinct)
- Delete useless test classes
This commit is contained in:
ljacqu 2016-04-03 07:38:13 +02:00
parent ba217a2595
commit c079692f1d
29 changed files with 67 additions and 225 deletions

View File

@ -17,13 +17,14 @@ import static fr.xephi.authme.command.FoundResultStatus.NO_PERMISSION;
import static fr.xephi.authme.command.FoundResultStatus.SUCCESS; import static fr.xephi.authme.command.FoundResultStatus.SUCCESS;
import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL; import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyListOf; import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -95,11 +96,8 @@ public class CommandHandlerTest {
// then // then
verify(serviceMock).mapPartsToCommand(eq(sender), captor.capture()); verify(serviceMock).mapPartsToCommand(eq(sender), captor.capture());
assertThat(captor.getValue(), contains("unreg", "testPlayer")); assertThat(captor.getValue(), contains("unreg", "testPlayer"));
verify(command, never()).getExecutableCommand(); verify(command, never()).getExecutableCommand();
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(sender).sendMessage(argThat(containsString("don't have permission")));
verify(sender).sendMessage(captor.capture());
assertThat(captor.getValue(), containsString("don't have permission"));
} }
@Test @Test
@ -170,11 +168,8 @@ public class CommandHandlerTest {
// then // then
verify(serviceMock).mapPartsToCommand(eq(sender), captor.capture()); verify(serviceMock).mapPartsToCommand(eq(sender), captor.capture());
assertThat(captor.getValue(), contains("unreg", "testPlayer")); assertThat(captor.getValue(), contains("unreg", "testPlayer"));
verify(command, never()).getExecutableCommand(); verify(command, never()).getExecutableCommand();
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(sender).sendMessage(argThat(containsString("Failed to parse")));
verify(sender).sendMessage(captor.capture());
assertThat(captor.getValue(), containsString("Failed to parse"));
} }
@Test @Test

View File

@ -3,7 +3,6 @@ package fr.xephi.authme.command;
import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.WrapperMock;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -38,7 +37,6 @@ public class CommandInitializerTest {
@BeforeClass @BeforeClass
public static void initializeCommandManager() { public static void initializeCommandManager() {
WrapperMock.createInstance();
commands = CommandInitializer.buildCommands(); commands = CommandInitializer.buildCommands();
} }

View File

@ -13,12 +13,12 @@ import java.util.Set;
import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel; import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
@ -31,7 +31,7 @@ public class CommandMapperTest {
private static Set<CommandDescription> commands; private static Set<CommandDescription> commands;
private static CommandMapper mapper; private static CommandMapper mapper;
private static PermissionsManager permissionsManagerMock; private static PermissionsManager permissionsManager;
@BeforeClass @BeforeClass
public static void setUpCommandHandler() { public static void setUpCommandHandler() {
@ -40,8 +40,8 @@ public class CommandMapperTest {
@Before @Before
public void setUpMocks() { public void setUpMocks() {
permissionsManagerMock = mock(PermissionsManager.class); permissionsManager = mock(PermissionsManager.class);
mapper = new CommandMapper(commands, permissionsManagerMock); mapper = new CommandMapper(commands, permissionsManager);
} }
// ----------- // -----------
@ -52,7 +52,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = Arrays.asList("authme", "login", "test1"); List<String> parts = Arrays.asList("authme", "login", "test1");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -71,7 +71,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = Arrays.asList("Authme", "REG", "arg1", "arg2"); List<String> parts = Arrays.asList("Authme", "REG", "arg1", "arg2");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -89,7 +89,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = Arrays.asList("authme", "register", "pass123", "pass123", "pass123"); List<String> parts = Arrays.asList("authme", "register", "pass123", "pass123", "pass123");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -107,7 +107,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = Arrays.asList("authme", "Reg"); List<String> parts = Arrays.asList("authme", "Reg");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -125,7 +125,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = Arrays.asList("authme", "reh", "pass123", "pass123"); List<String> parts = Arrays.asList("authme", "reh", "pass123", "pass123");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -144,7 +144,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = Arrays.asList("authme", "asdfawetawty4asdca"); List<String> parts = Arrays.asList("authme", "asdfawetawty4asdca");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -162,7 +162,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = singletonList("unregister"); List<String> parts = singletonList("unregister");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -180,7 +180,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = asList("bogus", "label1", "arg1"); List<String> parts = asList("bogus", "label1", "arg1");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -205,7 +205,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = asList("Unreg", "player1"); List<String> parts = asList("Unreg", "player1");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -223,7 +223,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = asList("unregistER", "player1", "wrongArg"); List<String> parts = asList("unregistER", "player1", "wrongArg");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -241,7 +241,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = asList("email", "helptest", "arg1"); List<String> parts = asList("email", "helptest", "arg1");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(true);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
@ -259,7 +259,7 @@ public class CommandMapperTest {
// given // given
List<String> parts = Arrays.asList("authme", "login", "test1"); List<String> parts = Arrays.asList("authme", "login", "test1");
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
given(permissionsManagerMock.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(false); given(permissionsManager.hasPermission(eq(sender), any(CommandDescription.class))).willReturn(false);
// when // when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);

View File

@ -28,8 +28,8 @@ import org.mockito.runners.MockitoJUnitRunner;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;

View File

@ -4,14 +4,13 @@ import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -28,12 +27,10 @@ public class PlayerCommandTest {
PlayerCommandImpl command = new PlayerCommandImpl(); PlayerCommandImpl command = new PlayerCommandImpl();
// when // when
command.executeCommand(sender, Collections.<String> emptyList(), mock(CommandService.class)); command.executeCommand(sender, Collections.<String>emptyList(), mock(CommandService.class));
// then // then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(sender).sendMessage(argThat(containsString("only for players")));
verify(sender, times(1)).sendMessage(captor.capture());
assertThat(captor.getValue(), containsString("only for players"));
} }
@Test @Test
@ -58,12 +55,10 @@ public class PlayerCommandTest {
PlayerCommandWithAlt command = new PlayerCommandWithAlt(); PlayerCommandWithAlt command = new PlayerCommandWithAlt();
// when // when
command.executeCommand(sender, Collections.<String> emptyList(), mock(CommandService.class)); command.executeCommand(sender, Collections.<String>emptyList(), mock(CommandService.class));
// then // then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(sender, times(1)).sendMessage(argThat(containsString("use /authme test <command> instead")));
verify(sender, times(1)).sendMessage(captor.capture());
assertThat(captor.getValue(), containsString("use /authme test <command> instead"));
} }

View File

@ -14,8 +14,8 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import static fr.xephi.authme.TestHelper.runInnerRunnable; import static fr.xephi.authme.TestHelper.runInnerRunnable;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -81,7 +81,7 @@ public class AccountsCommandTest {
// given // given
List<String> arguments = Collections.singletonList("SomeUser"); List<String> arguments = Collections.singletonList("SomeUser");
given(dataSource.getAuth("someuser")).willReturn(mock(PlayerAuth.class)); given(dataSource.getAuth("someuser")).willReturn(mock(PlayerAuth.class));
given(dataSource.getAllAuthsByIp(anyString())).willReturn(Collections.<String> emptyList()); given(dataSource.getAllAuthsByIp(anyString())).willReturn(Collections.<String>emptyList());
// when // when
command.executeCommand(sender, arguments, service); command.executeCommand(sender, arguments, service);
@ -115,7 +115,7 @@ public class AccountsCommandTest {
public void shouldReturnIpUnknown() { public void shouldReturnIpUnknown() {
// given // given
List<String> arguments = Collections.singletonList("123.45.67.89"); List<String> arguments = Collections.singletonList("123.45.67.89");
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.<String> emptyList()); given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.<String>emptyList());
// when // when
command.executeCommand(sender, arguments, service); command.executeCommand(sender, arguments, service);

View File

@ -8,7 +8,7 @@ import org.mockito.ArgumentCaptor;
import java.util.Collections; import java.util.Collections;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;

View File

@ -6,14 +6,13 @@ import fr.xephi.authme.settings.SpawnLoader;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.util.Collections; import java.util.Collections;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
@ -36,7 +35,7 @@ public class FirstSpawnCommandTest {
ExecutableCommand command = new FirstSpawnCommand(); ExecutableCommand command = new FirstSpawnCommand();
// when // when
command.executeCommand(player, Collections.<String> emptyList(), service); command.executeCommand(player, Collections.<String>emptyList(), service);
// then // then
verify(player).teleport(firstSpawn); verify(player).teleport(firstSpawn);
@ -54,12 +53,10 @@ public class FirstSpawnCommandTest {
ExecutableCommand command = new FirstSpawnCommand(); ExecutableCommand command = new FirstSpawnCommand();
// when // when
command.executeCommand(player, Collections.<String> emptyList(), service); command.executeCommand(player, Collections.<String>emptyList(), service);
// then // then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(player).sendMessage(argThat(containsString("spawn has failed")));
verify(player).sendMessage(captor.capture());
assertThat(captor.getValue(), containsString("spawn has failed"));
verify(player, never()).teleport(any(Location.class)); verify(player, never()).teleport(any(Location.class));
} }
} }

View File

@ -7,13 +7,12 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.util.Collections; import java.util.Collections;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -61,8 +60,6 @@ public class GetEmailCommandTest {
command.executeCommand(sender, Collections.singletonList(user), service); command.executeCommand(sender, Collections.singletonList(user), service);
// then // then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(sender).sendMessage(argThat(containsString(email)));
verify(sender).sendMessage(captor.capture());
assertThat(captor.getValue(), containsString(email));
} }
} }

View File

@ -103,7 +103,7 @@ public class LastLoginCommandTest {
ExecutableCommand command = new LastLoginCommand(); ExecutableCommand command = new LastLoginCommand();
// when // when
command.executeCommand(sender, Collections.<String> emptyList(), service); command.executeCommand(sender, Collections.<String>emptyList(), service);
// then // then
verify(dataSource).getAuth(name); verify(dataSource).getAuth(name);

View File

@ -61,7 +61,7 @@ public class PurgeLastPositionCommandTest {
ExecutableCommand command = new PurgeLastPositionCommand(); ExecutableCommand command = new PurgeLastPositionCommand();
// when // when
command.executeCommand(sender, Collections.<String> emptyList(), service); command.executeCommand(sender, Collections.<String>emptyList(), service);
// then // then
verify(dataSource).getAuth(player); verify(dataSource).getAuth(player);

View File

@ -37,7 +37,7 @@ public class ReloadCommandTest {
ExecutableCommand command = new ReloadCommand(); ExecutableCommand command = new ReloadCommand();
// when // when
command.executeCommand(sender, Collections.<String> emptyList(), service); command.executeCommand(sender, Collections.<String>emptyList(), service);
// then // then
verify(authMe).reload(); verify(authMe).reload();
@ -55,7 +55,7 @@ public class ReloadCommandTest {
ExecutableCommand command = new ReloadCommand(); ExecutableCommand command = new ReloadCommand();
// when // when
command.executeCommand(sender, Collections.<String> emptyList(), service); command.executeCommand(sender, Collections.<String>emptyList(), service);
// then // then
verify(authMe).reload(); verify(authMe).reload();

View File

@ -35,7 +35,7 @@ public class SetFirstSpawnCommandTest {
ExecutableCommand command = new SetFirstSpawnCommand(); ExecutableCommand command = new SetFirstSpawnCommand();
// when // when
command.executeCommand(player, Collections.<String> emptyList(), service); command.executeCommand(player, Collections.<String>emptyList(), service);
// then // then
verify(spawnLoader).setFirstSpawn(location); verify(spawnLoader).setFirstSpawn(location);
@ -57,7 +57,7 @@ public class SetFirstSpawnCommandTest {
ExecutableCommand command = new SetFirstSpawnCommand(); ExecutableCommand command = new SetFirstSpawnCommand();
// when // when
command.executeCommand(player, Collections.<String> emptyList(), service); command.executeCommand(player, Collections.<String>emptyList(), service);
// then // then
verify(spawnLoader).setFirstSpawn(location); verify(spawnLoader).setFirstSpawn(location);

View File

@ -35,7 +35,7 @@ public class SetSpawnCommandTest {
ExecutableCommand command = new SetSpawnCommand(); ExecutableCommand command = new SetSpawnCommand();
// when // when
command.executeCommand(player, Collections.<String> emptyList(), service); command.executeCommand(player, Collections.<String>emptyList(), service);
// then // then
verify(spawnLoader).setSpawn(location); verify(spawnLoader).setSpawn(location);
@ -57,7 +57,7 @@ public class SetSpawnCommandTest {
ExecutableCommand command = new SetSpawnCommand(); ExecutableCommand command = new SetSpawnCommand();
// when // when
command.executeCommand(player, Collections.<String> emptyList(), service); command.executeCommand(player, Collections.<String>emptyList(), service);
// then // then
verify(spawnLoader).setSpawn(location); verify(spawnLoader).setSpawn(location);

View File

@ -35,7 +35,7 @@ public class SpawnCommandTest {
ExecutableCommand command = new SpawnCommand(); ExecutableCommand command = new SpawnCommand();
// when // when
command.executeCommand(player, Collections.<String> emptyList(), service); command.executeCommand(player, Collections.<String>emptyList(), service);
// then // then
verify(player).teleport(spawn); verify(player).teleport(spawn);
@ -53,7 +53,7 @@ public class SpawnCommandTest {
ExecutableCommand command = new SpawnCommand(); ExecutableCommand command = new SpawnCommand();
// when // when
command.executeCommand(player, Collections.<String> emptyList(), service); command.executeCommand(player, Collections.<String>emptyList(), service);
// then // then
verify(player).sendMessage(argThat(containsString("Spawn has failed"))); verify(player).sendMessage(argThat(containsString("Spawn has failed")));

View File

@ -1,75 +0,0 @@
package fr.xephi.authme.command.executable.captcha;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.WrapperMock;
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 java.util.ArrayList;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link CaptchaCommand}.
*/
public class CaptchaCommandTest {
private WrapperMock wrapperMock;
private CommandService commandService;
@Before
public void setUpWrapperMock() {
wrapperMock = WrapperMock.createInstance();
commandService = mock(CommandService.class);
given(commandService.getProperty(SecuritySettings.USE_CAPTCHA)).willReturn(true);
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);
ExecutableCommand command = new CaptchaCommand();
// when
command.executeCommand(sender, new ArrayList<String>(), commandService);
// then
assertThat(wrapperMock.wasMockCalled(AuthMe.class), equalTo(false));
assertThat(wrapperMock.wasMockCalled(Messages.class), equalTo(false));
}
@Test
public void shouldRejectIfCaptchaIsNotUsed() {
// given
Player player = mockPlayerWithName("testplayer");
ExecutableCommand command = new CaptchaCommand();
given(commandService.getProperty(SecuritySettings.USE_CAPTCHA)).willReturn(false);
// when
command.executeCommand(player, Collections.singletonList("1234"), commandService);
// then
verify(commandService).send(player, MessageKey.USAGE_LOGIN);
}
private static Player mockPlayerWithName(String name) {
Player player = Mockito.mock(Player.class);
when(player.getName()).thenReturn(name);
return player;
}
}

View File

@ -18,9 +18,9 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.any; import static org.mockito.Mockito.any;

View File

@ -3,7 +3,6 @@ package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -29,7 +28,6 @@ public class AddEmailCommandTest {
@Before @Before
public void setUpMocks() { public void setUpMocks() {
commandService = mock(CommandService.class); commandService = mock(CommandService.class);
WrapperMock.createInstance();
} }
@Test @Test

View File

@ -1,41 +0,0 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import static org.mockito.Mockito.mock;
/**
* Test for {@link RecoverEmailCommand}.
*/
public class RecoverEmailCommandTest {
@Before
public void setUpMocks() {
WrapperMock wrapper = WrapperMock.createInstance();
}
@Test
@Ignore
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);
RecoverEmailCommand command = new RecoverEmailCommand();
// when
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class));
// then
}
// TODO ljacqu 20151121: Expand tests. This command doesn't use a scheduler and has all of its
// logic inside here.
}

View File

@ -2,22 +2,20 @@ package fr.xephi.authme.command.executable.login;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
@ -26,17 +24,12 @@ import static org.mockito.Mockito.verify;
/** /**
* Test for {@link LoginCommand}. * Test for {@link LoginCommand}.
*/ */
@RunWith(MockitoJUnitRunner.class)
public class LoginCommandTest { public class LoginCommandTest {
@Mock
private CommandService commandService; private CommandService commandService;
@Before
public void initializeAuthMeMock() {
WrapperMock.createInstance();
Settings.captchaLength = 10;
commandService = mock(CommandService.class);
}
@Test @Test
public void shouldStopIfSenderIsNotAPlayer() { public void shouldStopIfSenderIsNotAPlayer() {
// given // given
@ -47,10 +40,8 @@ public class LoginCommandTest {
command.executeCommand(sender, new ArrayList<String>(), commandService); command.executeCommand(sender, new ArrayList<String>(), commandService);
// then // then
Mockito.verify(commandService, never()).getManagement(); verify(commandService, never()).getManagement();
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); verify(sender).sendMessage(argThat(containsString("only for players")));
verify(sender).sendMessage(messageCaptor.capture());
assertThat(messageCaptor.getValue(), containsString("only for players"));
} }
@Test @Test
@ -65,7 +56,7 @@ public class LoginCommandTest {
command.executeCommand(sender, Collections.singletonList("password"), commandService); command.executeCommand(sender, Collections.singletonList("password"), commandService);
// then // then
Mockito.verify(management).performLogin(eq(sender), eq("password"), eq(false)); verify(management).performLogin(eq(sender), eq("password"), eq(false));
} }
} }

View File

@ -2,21 +2,18 @@ package fr.xephi.authme.command.executable.logout;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -30,8 +27,6 @@ public class LogoutCommandTest {
@Before @Before
public void initializeAuthMeMock() { public void initializeAuthMeMock() {
WrapperMock.createInstance();
Settings.captchaLength = 10;
commandService = mock(CommandService.class); commandService = mock(CommandService.class);
} }
@ -46,9 +41,7 @@ public class LogoutCommandTest {
// then // then
verify(commandService, never()).getManagement(); verify(commandService, never()).getManagement();
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); verify(sender).sendMessage(argThat(containsString("only for players")));
verify(sender).sendMessage(messageCaptor.capture());
assertThat(messageCaptor.getValue(), containsString("only for players"));
} }
@Test @Test

View File

@ -10,14 +10,13 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -47,9 +46,7 @@ public class RegisterCommandTest {
// then // then
verify(commandService, never()).getManagement(); verify(commandService, never()).getManagement();
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); verify(sender).sendMessage(argThat(containsString("Player only!")));
verify(sender).sendMessage(messageCaptor.capture());
assertThat(messageCaptor.getValue(), containsString("Player only!"));
} }
@Test @Test

View File

@ -6,7 +6,6 @@ import fr.xephi.authme.command.FoundResultStatus;
import fr.xephi.authme.command.TestCommandsUtil; import fr.xephi.authme.command.TestCommandsUtil;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission; import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.junit.Before; import org.junit.Before;
@ -47,7 +46,6 @@ public class HelpProviderTest {
@BeforeClass @BeforeClass
public static void setUpCommands() { public static void setUpCommands() {
WrapperMock.createInstance();
commands = TestCommandsUtil.generateCommands(); commands = TestCommandsUtil.generateCommands();
} }

View File

@ -16,8 +16,8 @@ import java.util.List;
import static fr.xephi.authme.AuthMeMatchers.equalToHash; import static fr.xephi.authme.AuthMeMatchers.equalToHash;
import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData; import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData;
import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation; import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;

View File

@ -13,7 +13,6 @@ import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.ValidationService; import fr.xephi.authme.util.ValidationService;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.hamcrest.MatcherAssert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -200,7 +199,7 @@ public class ProcessServiceTest {
MessageKey result = processService.validatePassword(password, user); MessageKey result = processService.validatePassword(password, user);
// then // then
MatcherAssert.assertThat(result, equalTo(MessageKey.PASSWORD_MATCH_ERROR)); assertThat(result, equalTo(MessageKey.PASSWORD_MATCH_ERROR));
verify(validationService).validatePassword(password, user); verify(validationService).validatePassword(password, user);
} }

View File

@ -6,9 +6,9 @@ import java.security.MessageDigest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/** /**
* Test for {@link HashUtils}. * Test for {@link HashUtils}.

View File

@ -4,8 +4,8 @@ import org.junit.Test;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/** /**
* Test for {@link RandomString}. * Test for {@link RandomString}.

View File

@ -21,12 +21,12 @@ import java.util.List;
import static fr.xephi.authme.settings.properties.PluginSettings.MESSAGES_LANGUAGE; import static fr.xephi.authme.settings.properties.PluginSettings.MESSAGES_LANGUAGE;
import static fr.xephi.authme.util.StringUtils.makePath; import static fr.xephi.authme.util.StringUtils.makePath;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyDouble; import static org.mockito.Matchers.anyDouble;

View File

@ -8,9 +8,9 @@ import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;