Refactor util for setting BukkitService mock behavior

- Move helper methods for setting BukkitService mock behavior into their own class
- Change methods to use Mockito's answer instead of verification + argument capture -> calling the methods now belongs to the test setup (given clause) and allows the behavior to take effect more than once
This commit is contained in:
ljacqu
2017-11-22 00:24:11 +01:00
parent 4717dc148c
commit 1053440b15
13 changed files with 147 additions and 139 deletions
@@ -17,7 +17,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static fr.xephi.authme.TestHelper.runInnerRunnable;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskAsynchronously;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
@@ -50,10 +50,10 @@ public class AccountsCommandTest {
List<String> arguments = Collections.emptyList();
given(dataSource.getAuth("tester")).willReturn(authWithIp("123.45.67.89"));
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Arrays.asList("Toaster", "Pester"));
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 2);
@@ -67,10 +67,10 @@ public class AccountsCommandTest {
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("SomeUser");
given(dataSource.getAuth("someuser")).willReturn(null);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
runInnerRunnable(bukkitService);
// then
verify(service).send(sender, MessageKey.UNKNOWN_USER);
@@ -84,10 +84,10 @@ public class AccountsCommandTest {
List<String> arguments = Collections.singletonList("SomeUser");
PlayerAuth auth = authWithIp("144.56.77.88");
given(dataSource.getAuth("someuser")).willReturn(auth);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
runInnerRunnable(bukkitService);
// then
verify(service).send(sender, MessageKey.UNKNOWN_USER);
@@ -101,10 +101,10 @@ public class AccountsCommandTest {
List<String> arguments = Collections.singletonList("SomeUser");
given(dataSource.getAuth("someuser")).willReturn(authWithIp("56.78.90.123"));
given(dataSource.getAllAuthsByIp("56.78.90.123")).willReturn(Collections.singletonList("SomeUser"));
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 1);
@@ -120,10 +120,10 @@ public class AccountsCommandTest {
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("123.45.67.89");
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.emptyList());
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 1);
@@ -136,10 +136,10 @@ public class AccountsCommandTest {
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("24.24.48.48");
given(dataSource.getAllAuthsByIp("24.24.48.48")).willReturn(Collections.singletonList("SomeUser"));
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 1);
@@ -152,10 +152,10 @@ public class AccountsCommandTest {
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("98.76.41.122");
given(dataSource.getAllAuthsByIp("98.76.41.122")).willReturn(Arrays.asList("Tester", "Lester", "Taster"));
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 2);
@@ -20,6 +20,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskAsynchronously;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -105,10 +106,10 @@ public class ConverterCommandTest {
Class<? extends Converter> converterClass = ConverterCommand.CONVERTERS.get(converterName);
Converter converter = createMockReturnedByInjector(converterClass);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, Collections.singletonList(converterName));
TestHelper.runInnerRunnable(bukkitService);
// then
verify(converter).execute(sender);
@@ -125,10 +126,10 @@ public class ConverterCommandTest {
Converter converter = createMockReturnedByInjector(converterClass);
doThrow(IllegalStateException.class).when(converter).execute(any(CommandSender.class));
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, Collections.singletonList(converterName.toUpperCase()));
TestHelper.runInnerRunnable(bukkitService);
// then
verify(converter).execute(sender);
@@ -11,7 +11,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static fr.xephi.authme.TestHelper.runOptionallyAsyncTask;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskOptionallyAsync;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.containsString;
@@ -45,10 +45,10 @@ public class PurgePlayerCommandTest {
String name = "Bobby";
given(dataSource.isAuthAvailable(name)).willReturn(true);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, singletonList(name));
runOptionallyAsyncTask(bukkitService);
// then
verify(sender).sendMessage(argThat(containsString("This player is still registered")));
@@ -63,10 +63,10 @@ public class PurgePlayerCommandTest {
OfflinePlayer player = mock(OfflinePlayer.class);
given(bukkitService.getOfflinePlayer(name)).willReturn(player);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, singletonList(name));
runOptionallyAsyncTask(bukkitService);
// then
verify(dataSource).isAuthAvailable(name);
@@ -80,10 +80,10 @@ public class PurgePlayerCommandTest {
OfflinePlayer player = mock(OfflinePlayer.class);
given(bukkitService.getOfflinePlayer(name)).willReturn(player);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, asList(name, "force"));
runOptionallyAsyncTask(bukkitService);
// then
verify(purgeExecutor).executePurge(singletonList(player), singletonList(name.toLowerCase()));
@@ -22,7 +22,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import static fr.xephi.authme.TestHelper.setBukkitServiceToRunOptionallyAsyncTasks;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskOptionallyAsync;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
@@ -86,10 +87,10 @@ public class RegisterAdminCommandTest {
given(validationService.validatePassword(password, user)).willReturn(new ValidationResult());
given(dataSource.isAuthAvailable(user)).willReturn(true);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, password));
TestHelper.runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, user);
@@ -108,10 +109,10 @@ public class RegisterAdminCommandTest {
HashedPassword hashedPassword = new HashedPassword("235sdf4w5udsgf");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, password));
TestHelper.runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, user);
@@ -133,10 +134,10 @@ public class RegisterAdminCommandTest {
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
given(bukkitService.getPlayerExact(user)).willReturn(null);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, password));
TestHelper.runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, user);
@@ -161,11 +162,11 @@ public class RegisterAdminCommandTest {
String kickForAdminRegister = "Admin registered you -- log in again";
given(commandService.retrieveSingleMessage(MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, password));
TestHelper.runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, user);
@@ -16,7 +16,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import static fr.xephi.authme.TestHelper.runOptionallyAsyncTask;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskOptionallyAsync;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -74,10 +74,10 @@ public class SetEmailCommandTest {
given(validationService.validateEmail(email)).willReturn(true);
given(dataSource.getAuth(user)).willReturn(null);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validateEmail(email);
@@ -96,10 +96,10 @@ public class SetEmailCommandTest {
given(dataSource.getAuth(user)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(false);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validateEmail(email);
@@ -121,10 +121,10 @@ public class SetEmailCommandTest {
CommandSender sender = mock(CommandSender.class);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(dataSource.updateEmail(auth)).willReturn(false);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validateEmail(email);
@@ -147,10 +147,10 @@ public class SetEmailCommandTest {
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(dataSource.updateEmail(auth)).willReturn(true);
given(playerCache.getAuth(user)).willReturn(null);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validateEmail(email);
@@ -174,10 +174,10 @@ public class SetEmailCommandTest {
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(dataSource.updateEmail(auth)).willReturn(true);
given(playerCache.getAuth(user)).willReturn(mock(PlayerAuth.class));
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validateEmail(email);