From c079692f1d1ee252ef22487881d2b64a543fbc84 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 3 Apr 2016 07:38:13 +0200 Subject: [PATCH] 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 --- .../authme/command/CommandHandlerTest.java | 13 +--- .../command/CommandInitializerTest.java | 2 - .../authme/command/CommandMapperTest.java | 32 ++++---- .../authme/command/CommandServiceTest.java | 2 +- .../authme/command/PlayerCommandTest.java | 15 ++-- .../authme/AccountsCommandTest.java | 8 +- .../executable/authme/AuthMeCommandTest.java | 2 +- .../authme/FirstSpawnCommandTest.java | 11 +-- .../authme/GetEmailCommandTest.java | 7 +- .../authme/LastLoginCommandTest.java | 2 +- .../authme/PurgeLastPositionCommandTest.java | 2 +- .../executable/authme/ReloadCommandTest.java | 4 +- .../authme/SetFirstSpawnCommandTest.java | 4 +- .../authme/SetSpawnCommandTest.java | 4 +- .../executable/authme/SpawnCommandTest.java | 4 +- .../captcha/CaptchaCommandTest.java | 75 ------------------- .../ChangePasswordCommandTest.java | 2 +- .../executable/email/AddEmailCommandTest.java | 2 - .../email/RecoverEmailCommandTest.java | 41 ---------- .../executable/login/LoginCommandTest.java | 27 +++---- .../executable/logout/LogoutCommandTest.java | 11 +-- .../register/RegisterCommandTest.java | 7 +- .../authme/command/help/HelpProviderTest.java | 2 - .../datasource/FlatFileIntegrationTest.java | 2 +- .../authme/process/ProcessServiceTest.java | 3 +- .../xephi/authme/security/HashUtilsTest.java | 2 +- .../authme/security/RandomStringTest.java | 2 +- .../xephi/authme/settings/NewSettingTest.java | 2 +- .../authme/settings/domain/PropertyTest.java | 2 +- 29 files changed, 67 insertions(+), 225 deletions(-) delete mode 100644 src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java delete mode 100644 src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java diff --git a/src/test/java/fr/xephi/authme/command/CommandHandlerTest.java b/src/test/java/fr/xephi/authme/command/CommandHandlerTest.java index 345082bc9..e5e6b7496 100644 --- a/src/test/java/fr/xephi/authme/command/CommandHandlerTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandHandlerTest.java @@ -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.UNKNOWN_LABEL; import static java.util.Arrays.asList; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyListOf; import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; @@ -95,11 +96,8 @@ public class CommandHandlerTest { // then verify(serviceMock).mapPartsToCommand(eq(sender), captor.capture()); assertThat(captor.getValue(), contains("unreg", "testPlayer")); - verify(command, never()).getExecutableCommand(); - ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); - verify(sender).sendMessage(captor.capture()); - assertThat(captor.getValue(), containsString("don't have permission")); + verify(sender).sendMessage(argThat(containsString("don't have permission"))); } @Test @@ -170,11 +168,8 @@ public class CommandHandlerTest { // then verify(serviceMock).mapPartsToCommand(eq(sender), captor.capture()); assertThat(captor.getValue(), contains("unreg", "testPlayer")); - verify(command, never()).getExecutableCommand(); - ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); - verify(sender).sendMessage(captor.capture()); - assertThat(captor.getValue(), containsString("Failed to parse")); + verify(sender).sendMessage(argThat(containsString("Failed to parse"))); } @Test diff --git a/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java b/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java index 7e62023f1..6e5418d5e 100644 --- a/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java @@ -3,7 +3,6 @@ package fr.xephi.authme.command; import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.util.StringUtils; -import fr.xephi.authme.util.WrapperMock; import org.junit.BeforeClass; import org.junit.Test; @@ -38,7 +37,6 @@ public class CommandInitializerTest { @BeforeClass public static void initializeCommandManager() { - WrapperMock.createInstance(); commands = CommandInitializer.buildCommands(); } diff --git a/src/test/java/fr/xephi/authme/command/CommandMapperTest.java b/src/test/java/fr/xephi/authme/command/CommandMapperTest.java index 8da420431..77840e89e 100644 --- a/src/test/java/fr/xephi/authme/command/CommandMapperTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandMapperTest.java @@ -13,12 +13,12 @@ import java.util.Set; import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; @@ -31,7 +31,7 @@ public class CommandMapperTest { private static Set commands; private static CommandMapper mapper; - private static PermissionsManager permissionsManagerMock; + private static PermissionsManager permissionsManager; @BeforeClass public static void setUpCommandHandler() { @@ -40,8 +40,8 @@ public class CommandMapperTest { @Before public void setUpMocks() { - permissionsManagerMock = mock(PermissionsManager.class); - mapper = new CommandMapper(commands, permissionsManagerMock); + permissionsManager = mock(PermissionsManager.class); + mapper = new CommandMapper(commands, permissionsManager); } // ----------- @@ -52,7 +52,7 @@ public class CommandMapperTest { // given List parts = Arrays.asList("authme", "login", "test1"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -71,7 +71,7 @@ public class CommandMapperTest { // given List parts = Arrays.asList("Authme", "REG", "arg1", "arg2"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -89,7 +89,7 @@ public class CommandMapperTest { // given List parts = Arrays.asList("authme", "register", "pass123", "pass123", "pass123"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -107,7 +107,7 @@ public class CommandMapperTest { // given List parts = Arrays.asList("authme", "Reg"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -125,7 +125,7 @@ public class CommandMapperTest { // given List parts = Arrays.asList("authme", "reh", "pass123", "pass123"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -144,7 +144,7 @@ public class CommandMapperTest { // given List parts = Arrays.asList("authme", "asdfawetawty4asdca"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -162,7 +162,7 @@ public class CommandMapperTest { // given List parts = singletonList("unregister"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -180,7 +180,7 @@ public class CommandMapperTest { // given List parts = asList("bogus", "label1", "arg1"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -205,7 +205,7 @@ public class CommandMapperTest { // given List parts = asList("Unreg", "player1"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -223,7 +223,7 @@ public class CommandMapperTest { // given List parts = asList("unregistER", "player1", "wrongArg"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -241,7 +241,7 @@ public class CommandMapperTest { // given List parts = asList("email", "helptest", "arg1"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); @@ -259,7 +259,7 @@ public class CommandMapperTest { // given List parts = Arrays.asList("authme", "login", "test1"); 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 FoundCommandResult result = mapper.mapPartsToCommand(sender, parts); diff --git a/src/test/java/fr/xephi/authme/command/CommandServiceTest.java b/src/test/java/fr/xephi/authme/command/CommandServiceTest.java index dafe69667..645d3a372 100644 --- a/src/test/java/fr/xephi/authme/command/CommandServiceTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandServiceTest.java @@ -28,8 +28,8 @@ import org.mockito.runners.MockitoJUnitRunner; import java.util.Arrays; import java.util.List; -import static org.hamcrest.MatcherAssert.assertThat; 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.times; diff --git a/src/test/java/fr/xephi/authme/command/PlayerCommandTest.java b/src/test/java/fr/xephi/authme/command/PlayerCommandTest.java index 4afee564c..a4c02b93a 100644 --- a/src/test/java/fr/xephi/authme/command/PlayerCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/PlayerCommandTest.java @@ -4,14 +4,13 @@ import org.bukkit.command.BlockCommandSender; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.junit.Test; -import org.mockito.ArgumentCaptor; import java.util.Arrays; import java.util.Collections; import java.util.List; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -28,12 +27,10 @@ public class PlayerCommandTest { PlayerCommandImpl command = new PlayerCommandImpl(); // when - command.executeCommand(sender, Collections. emptyList(), mock(CommandService.class)); + command.executeCommand(sender, Collections.emptyList(), mock(CommandService.class)); // then - ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); - verify(sender, times(1)).sendMessage(captor.capture()); - assertThat(captor.getValue(), containsString("only for players")); + verify(sender).sendMessage(argThat(containsString("only for players"))); } @Test @@ -58,12 +55,10 @@ public class PlayerCommandTest { PlayerCommandWithAlt command = new PlayerCommandWithAlt(); // when - command.executeCommand(sender, Collections. emptyList(), mock(CommandService.class)); + command.executeCommand(sender, Collections.emptyList(), mock(CommandService.class)); // then - ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); - verify(sender, times(1)).sendMessage(captor.capture()); - assertThat(captor.getValue(), containsString("use /authme test instead")); + verify(sender, times(1)).sendMessage(argThat(containsString("use /authme test instead"))); } diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/AccountsCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/AccountsCommandTest.java index 824696354..5a837280d 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/AccountsCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/AccountsCommandTest.java @@ -14,8 +14,8 @@ import java.util.Collections; import java.util.List; import static fr.xephi.authme.TestHelper.runInnerRunnable; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; @@ -81,7 +81,7 @@ public class AccountsCommandTest { // given List arguments = Collections.singletonList("SomeUser"); given(dataSource.getAuth("someuser")).willReturn(mock(PlayerAuth.class)); - given(dataSource.getAllAuthsByIp(anyString())).willReturn(Collections. emptyList()); + given(dataSource.getAllAuthsByIp(anyString())).willReturn(Collections.emptyList()); // when command.executeCommand(sender, arguments, service); @@ -115,7 +115,7 @@ public class AccountsCommandTest { public void shouldReturnIpUnknown() { // given List arguments = Collections.singletonList("123.45.67.89"); - given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections. emptyList()); + given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.emptyList()); // when command.executeCommand(sender, arguments, service); diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/AuthMeCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/AuthMeCommandTest.java index 8be1185ab..c1c0c6e3a 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/AuthMeCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/AuthMeCommandTest.java @@ -8,7 +8,7 @@ import org.mockito.ArgumentCaptor; 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.mockito.Mockito.mock; import static org.mockito.Mockito.times; diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/FirstSpawnCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/FirstSpawnCommandTest.java index aa4f7b74b..0c70bd01b 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/FirstSpawnCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/FirstSpawnCommandTest.java @@ -6,14 +6,13 @@ import fr.xephi.authme.settings.SpawnLoader; import org.bukkit.Location; import org.bukkit.entity.Player; import org.junit.Test; -import org.mockito.ArgumentCaptor; import java.util.Collections; import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -36,7 +35,7 @@ public class FirstSpawnCommandTest { ExecutableCommand command = new FirstSpawnCommand(); // when - command.executeCommand(player, Collections. emptyList(), service); + command.executeCommand(player, Collections.emptyList(), service); // then verify(player).teleport(firstSpawn); @@ -54,12 +53,10 @@ public class FirstSpawnCommandTest { ExecutableCommand command = new FirstSpawnCommand(); // when - command.executeCommand(player, Collections. emptyList(), service); + command.executeCommand(player, Collections.emptyList(), service); // then - ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); - verify(player).sendMessage(captor.capture()); - assertThat(captor.getValue(), containsString("spawn has failed")); + verify(player).sendMessage(argThat(containsString("spawn has failed"))); verify(player, never()).teleport(any(Location.class)); } } diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/GetEmailCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/GetEmailCommandTest.java index 247c43ac9..6d2320377 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/GetEmailCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/GetEmailCommandTest.java @@ -7,13 +7,12 @@ import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; import org.bukkit.command.CommandSender; import org.junit.Test; -import org.mockito.ArgumentCaptor; import java.util.Collections; import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -61,8 +60,6 @@ public class GetEmailCommandTest { command.executeCommand(sender, Collections.singletonList(user), service); // then - ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); - verify(sender).sendMessage(captor.capture()); - assertThat(captor.getValue(), containsString(email)); + verify(sender).sendMessage(argThat(containsString(email))); } } diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/LastLoginCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/LastLoginCommandTest.java index 96cf8844a..d1e58e92a 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/LastLoginCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/LastLoginCommandTest.java @@ -103,7 +103,7 @@ public class LastLoginCommandTest { ExecutableCommand command = new LastLoginCommand(); // when - command.executeCommand(sender, Collections. emptyList(), service); + command.executeCommand(sender, Collections.emptyList(), service); // then verify(dataSource).getAuth(name); diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommandTest.java index 9f2078667..bcce0342e 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommandTest.java @@ -61,7 +61,7 @@ public class PurgeLastPositionCommandTest { ExecutableCommand command = new PurgeLastPositionCommand(); // when - command.executeCommand(sender, Collections. emptyList(), service); + command.executeCommand(sender, Collections.emptyList(), service); // then verify(dataSource).getAuth(player); diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/ReloadCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/ReloadCommandTest.java index 873108ee0..a0ed7706e 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/ReloadCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/ReloadCommandTest.java @@ -37,7 +37,7 @@ public class ReloadCommandTest { ExecutableCommand command = new ReloadCommand(); // when - command.executeCommand(sender, Collections. emptyList(), service); + command.executeCommand(sender, Collections.emptyList(), service); // then verify(authMe).reload(); @@ -55,7 +55,7 @@ public class ReloadCommandTest { ExecutableCommand command = new ReloadCommand(); // when - command.executeCommand(sender, Collections. emptyList(), service); + command.executeCommand(sender, Collections.emptyList(), service); // then verify(authMe).reload(); diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/SetFirstSpawnCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/SetFirstSpawnCommandTest.java index 307831ce7..749648827 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/SetFirstSpawnCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/SetFirstSpawnCommandTest.java @@ -35,7 +35,7 @@ public class SetFirstSpawnCommandTest { ExecutableCommand command = new SetFirstSpawnCommand(); // when - command.executeCommand(player, Collections. emptyList(), service); + command.executeCommand(player, Collections.emptyList(), service); // then verify(spawnLoader).setFirstSpawn(location); @@ -57,7 +57,7 @@ public class SetFirstSpawnCommandTest { ExecutableCommand command = new SetFirstSpawnCommand(); // when - command.executeCommand(player, Collections. emptyList(), service); + command.executeCommand(player, Collections.emptyList(), service); // then verify(spawnLoader).setFirstSpawn(location); diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/SetSpawnCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/SetSpawnCommandTest.java index 11c28d32b..e3e01f99e 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/SetSpawnCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/SetSpawnCommandTest.java @@ -35,7 +35,7 @@ public class SetSpawnCommandTest { ExecutableCommand command = new SetSpawnCommand(); // when - command.executeCommand(player, Collections. emptyList(), service); + command.executeCommand(player, Collections.emptyList(), service); // then verify(spawnLoader).setSpawn(location); @@ -57,7 +57,7 @@ public class SetSpawnCommandTest { ExecutableCommand command = new SetSpawnCommand(); // when - command.executeCommand(player, Collections. emptyList(), service); + command.executeCommand(player, Collections.emptyList(), service); // then verify(spawnLoader).setSpawn(location); diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/SpawnCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/SpawnCommandTest.java index 7b76ad02b..799d1ca47 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/SpawnCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/SpawnCommandTest.java @@ -35,7 +35,7 @@ public class SpawnCommandTest { ExecutableCommand command = new SpawnCommand(); // when - command.executeCommand(player, Collections. emptyList(), service); + command.executeCommand(player, Collections.emptyList(), service); // then verify(player).teleport(spawn); @@ -53,7 +53,7 @@ public class SpawnCommandTest { ExecutableCommand command = new SpawnCommand(); // when - command.executeCommand(player, Collections. emptyList(), service); + command.executeCommand(player, Collections.emptyList(), service); // then verify(player).sendMessage(argThat(containsString("Spawn has failed"))); diff --git a/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java deleted file mode 100644 index b3b54a1c4..000000000 --- a/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java +++ /dev/null @@ -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(), 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; - } -} diff --git a/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java index ef4572055..bc8e0876a 100644 --- a/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java @@ -18,9 +18,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.any; diff --git a/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java index f1e55787f..17dbddad5 100644 --- a/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java @@ -3,7 +3,6 @@ package fr.xephi.authme.command.executable.email; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.process.Management; import fr.xephi.authme.settings.NewSetting; -import fr.xephi.authme.util.WrapperMock; import org.bukkit.command.BlockCommandSender; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -29,7 +28,6 @@ public class AddEmailCommandTest { @Before public void setUpMocks() { commandService = mock(CommandService.class); - WrapperMock.createInstance(); } @Test diff --git a/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java deleted file mode 100644 index 94c1c8dcc..000000000 --- a/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java +++ /dev/null @@ -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(), mock(CommandService.class)); - - // then - } - - // TODO ljacqu 20151121: Expand tests. This command doesn't use a scheduler and has all of its - // logic inside here. -} diff --git a/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java index 5e62866cb..9a23bc178 100644 --- a/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java @@ -2,22 +2,20 @@ package fr.xephi.authme.command.executable.login; import fr.xephi.authme.command.CommandService; 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.CommandSender; import org.bukkit.entity.Player; -import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; import java.util.ArrayList; import java.util.Collections; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -26,17 +24,12 @@ import static org.mockito.Mockito.verify; /** * Test for {@link LoginCommand}. */ +@RunWith(MockitoJUnitRunner.class) public class LoginCommandTest { + @Mock private CommandService commandService; - @Before - public void initializeAuthMeMock() { - WrapperMock.createInstance(); - Settings.captchaLength = 10; - commandService = mock(CommandService.class); - } - @Test public void shouldStopIfSenderIsNotAPlayer() { // given @@ -47,10 +40,8 @@ public class LoginCommandTest { command.executeCommand(sender, new ArrayList(), commandService); // then - Mockito.verify(commandService, never()).getManagement(); - ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); - verify(sender).sendMessage(messageCaptor.capture()); - assertThat(messageCaptor.getValue(), containsString("only for players")); + verify(commandService, never()).getManagement(); + verify(sender).sendMessage(argThat(containsString("only for players"))); } @Test @@ -65,7 +56,7 @@ public class LoginCommandTest { command.executeCommand(sender, Collections.singletonList("password"), commandService); // then - Mockito.verify(management).performLogin(eq(sender), eq("password"), eq(false)); + verify(management).performLogin(eq(sender), eq("password"), eq(false)); } } diff --git a/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java index 6e83e8016..7ac3ede65 100644 --- a/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java @@ -2,21 +2,18 @@ package fr.xephi.authme.command.executable.logout; import fr.xephi.authme.command.CommandService; 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.CommandSender; import org.bukkit.entity.Player; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentCaptor; import java.util.ArrayList; import java.util.Collections; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -30,8 +27,6 @@ public class LogoutCommandTest { @Before public void initializeAuthMeMock() { - WrapperMock.createInstance(); - Settings.captchaLength = 10; commandService = mock(CommandService.class); } @@ -46,9 +41,7 @@ public class LogoutCommandTest { // then verify(commandService, never()).getManagement(); - ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); - verify(sender).sendMessage(messageCaptor.capture()); - assertThat(messageCaptor.getValue(), containsString("only for players")); + verify(sender).sendMessage(argThat(containsString("only for players"))); } @Test diff --git a/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java index e4ff5ac01..84b797263 100644 --- a/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java @@ -10,14 +10,13 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentCaptor; import java.util.ArrayList; import java.util.Collections; import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -47,9 +46,7 @@ public class RegisterCommandTest { // then verify(commandService, never()).getManagement(); - ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); - verify(sender).sendMessage(messageCaptor.capture()); - assertThat(messageCaptor.getValue(), containsString("Player only!")); + verify(sender).sendMessage(argThat(containsString("Player only!"))); } @Test diff --git a/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java b/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java index 6568a773b..3ec47a4f4 100644 --- a/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java +++ b/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java @@ -6,7 +6,6 @@ import fr.xephi.authme.command.FoundResultStatus; import fr.xephi.authme.command.TestCommandsUtil; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PlayerPermission; -import fr.xephi.authme.util.WrapperMock; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.junit.Before; @@ -47,7 +46,6 @@ public class HelpProviderTest { @BeforeClass public static void setUpCommands() { - WrapperMock.createInstance(); commands = TestCommandsUtil.generateCommands(); } diff --git a/src/test/java/fr/xephi/authme/datasource/FlatFileIntegrationTest.java b/src/test/java/fr/xephi/authme/datasource/FlatFileIntegrationTest.java index 154511c2f..0c0ab8e1e 100644 --- a/src/test/java/fr/xephi/authme/datasource/FlatFileIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/datasource/FlatFileIntegrationTest.java @@ -16,8 +16,8 @@ import java.util.List; import static fr.xephi.authme.AuthMeMatchers.equalToHash; import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData; import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation; -import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertThat; diff --git a/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java b/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java index 23a87beef..14d2e3e23 100644 --- a/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java +++ b/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java @@ -13,7 +13,6 @@ import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.util.ValidationService; import org.bukkit.command.CommandSender; -import org.hamcrest.MatcherAssert; import org.junit.Before; import org.junit.Test; @@ -200,7 +199,7 @@ public class ProcessServiceTest { MessageKey result = processService.validatePassword(password, user); // then - MatcherAssert.assertThat(result, equalTo(MessageKey.PASSWORD_MATCH_ERROR)); + assertThat(result, equalTo(MessageKey.PASSWORD_MATCH_ERROR)); verify(validationService).validatePassword(password, user); } diff --git a/src/test/java/fr/xephi/authme/security/HashUtilsTest.java b/src/test/java/fr/xephi/authme/security/HashUtilsTest.java index 02237cde7..e515fdbba 100644 --- a/src/test/java/fr/xephi/authme/security/HashUtilsTest.java +++ b/src/test/java/fr/xephi/authme/security/HashUtilsTest.java @@ -6,9 +6,9 @@ import java.security.MessageDigest; import java.util.ArrayList; import java.util.List; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; /** * Test for {@link HashUtils}. diff --git a/src/test/java/fr/xephi/authme/security/RandomStringTest.java b/src/test/java/fr/xephi/authme/security/RandomStringTest.java index 71ea587c7..3582fb91f 100644 --- a/src/test/java/fr/xephi/authme/security/RandomStringTest.java +++ b/src/test/java/fr/xephi/authme/security/RandomStringTest.java @@ -4,8 +4,8 @@ import org.junit.Test; import java.util.regex.Pattern; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; /** * Test for {@link RandomString}. diff --git a/src/test/java/fr/xephi/authme/settings/NewSettingTest.java b/src/test/java/fr/xephi/authme/settings/NewSettingTest.java index 0c69919c1..fb050862d 100644 --- a/src/test/java/fr/xephi/authme/settings/NewSettingTest.java +++ b/src/test/java/fr/xephi/authme/settings/NewSettingTest.java @@ -21,12 +21,12 @@ import java.util.List; import static fr.xephi.authme.settings.properties.PluginSettings.MESSAGES_LANGUAGE; 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.equalTo; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyDouble; diff --git a/src/test/java/fr/xephi/authme/settings/domain/PropertyTest.java b/src/test/java/fr/xephi/authme/settings/domain/PropertyTest.java index de33c53ca..ff84c3932 100644 --- a/src/test/java/fr/xephi/authme/settings/domain/PropertyTest.java +++ b/src/test/java/fr/xephi/authme/settings/domain/PropertyTest.java @@ -8,9 +8,9 @@ import org.mockito.internal.stubbing.answers.ReturnsArgumentAt; import java.util.Arrays; import java.util.List; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString;