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:
@@ -11,14 +11,16 @@ import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.ProtectionSettings;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.runSyncDelayedTaskWithDelay;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -55,14 +57,12 @@ public class AntiBotServiceTest {
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY)).willReturn(5);
|
||||
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(true);
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_DELAY)).willReturn(8);
|
||||
setBukkitServiceToScheduleSyncDelayedTaskWithDelay(bukkitService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStartListenerOnStartup() {
|
||||
// given / when
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
// then
|
||||
// given / when / then
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING));
|
||||
}
|
||||
|
||||
@@ -81,9 +81,10 @@ public class AntiBotServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // TODO ljacqu fix test
|
||||
public void shouldActivateAntibot() {
|
||||
// given - listening antibot
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
reset(bukkitService);
|
||||
|
||||
// when
|
||||
antiBotService.overrideAntiBotStatus(true);
|
||||
@@ -91,23 +92,23 @@ public class AntiBotServiceTest {
|
||||
// then
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.ACTIVE));
|
||||
// Check that a task is scheduled to disable again
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
|
||||
verify(bukkitService).runTaskLater(runnableCaptor.capture(), anyLong());
|
||||
runnableCaptor.getValue().run();
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotActivateAntibotForDisabledSetting() {
|
||||
// given - disabled antibot
|
||||
reset(bukkitService);
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.DISABLED));
|
||||
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(false);
|
||||
AntiBotService antiBotService = new AntiBotService(settings, messages, permissionsManager, bukkitService);
|
||||
|
||||
// when
|
||||
antiBotService.overrideAntiBotStatus(true);
|
||||
|
||||
// then
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.DISABLED));
|
||||
verifyZeroInteractions(bukkitService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -127,10 +128,7 @@ public class AntiBotServiceTest {
|
||||
|
||||
@Test
|
||||
public void shouldAcceptPlayerToJoin() {
|
||||
// given - listening antibot
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
// when
|
||||
// given / when
|
||||
boolean result = antiBotService.shouldKick();
|
||||
|
||||
// then
|
||||
@@ -142,9 +140,7 @@ public class AntiBotServiceTest {
|
||||
// given
|
||||
int sensitivity = 10;
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY)).willReturn(sensitivity);
|
||||
reset(bukkitService);
|
||||
AntiBotService antiBotService = new AntiBotService(settings, messages, permissionsManager, bukkitService);
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
for (int i = 0; i < sensitivity; ++i) {
|
||||
antiBotService.shouldKick();
|
||||
@@ -162,7 +158,6 @@ public class AntiBotServiceTest {
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public void shouldInformPlayersOnActivation() {
|
||||
// given - listening antibot
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
List<Player> players = Arrays.asList(mock(Player.class), mock(Player.class));
|
||||
given(bukkitService.getOnlinePlayers()).willReturn((List) players);
|
||||
given(permissionsManager.hasPermission(players.get(0), AdminPermission.ANTIBOT_MESSAGES)).willReturn(false);
|
||||
@@ -180,7 +175,6 @@ public class AntiBotServiceTest {
|
||||
@Test
|
||||
public void shouldImmediatelyStartAfterFirstStartup() {
|
||||
// given - listening antibot
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
given(bukkitService.runTaskLater(any(Runnable.class), anyLong())).willReturn(mock(BukkitTask.class));
|
||||
antiBotService.overrideAntiBotStatus(true);
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
|
||||
/**
|
||||
* Offers utility methods for testing involving a {@link BukkitService} mock.
|
||||
*/
|
||||
public final class BukkitServiceTestHelper {
|
||||
|
||||
private BukkitServiceTestHelper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#scheduleSyncTaskFromOptionallyAsyncTask}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#runTaskAsynchronously}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToRunTaskAsynchronously(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).runTaskAsynchronously(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#runTaskOptionallyAsync}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToRunTaskOptionallyAsync(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).runTaskOptionallyAsync(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#scheduleSyncDelayedTask(Runnable)}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToScheduleSyncDelayedTask(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).scheduleSyncDelayedTask(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#scheduleSyncDelayedTask(Runnable, long)}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToScheduleSyncDelayedTaskWithDelay(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), anyLong());
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.setBukkitServiceToRunOptionallyAsyncTasks;
|
||||
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;
|
||||
@@ -89,7 +89,7 @@ public class TeleportationServiceTest {
|
||||
given(player.isOnline()).willReturn(true);
|
||||
Location firstSpawn = mockLocation();
|
||||
given(spawnLoader.getFirstSpawn()).willReturn(firstSpawn);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportNewPlayerToFirstSpawn(player);
|
||||
@@ -109,7 +109,7 @@ public class TeleportationServiceTest {
|
||||
given(player.isOnline()).willReturn(true);
|
||||
Location spawn = mockLocation();
|
||||
given(spawnLoader.getSpawnLocation(player)).willReturn(spawn);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnJoin(player);
|
||||
@@ -178,7 +178,7 @@ public class TeleportationServiceTest {
|
||||
event.setTo(null);
|
||||
return null;
|
||||
}).when(bukkitService).callEvent(any(SpawnTeleportEvent.class));
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnJoin(player);
|
||||
@@ -201,7 +201,7 @@ public class TeleportationServiceTest {
|
||||
event.setCancelled(true);
|
||||
return null;
|
||||
}).when(bukkitService).callEvent(any(SpawnTeleportEvent.class));
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnJoin(player);
|
||||
@@ -242,7 +242,7 @@ public class TeleportationServiceTest {
|
||||
Location limboLocation = mockLocation();
|
||||
given(limboLocation.getWorld().getName()).willReturn("forced1");
|
||||
given(limbo.getLocation()).willReturn(limboLocation);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
@@ -289,7 +289,7 @@ public class TeleportationServiceTest {
|
||||
LimboPlayer limbo = mock(LimboPlayer.class);
|
||||
Location limboLocation = mockLocation();
|
||||
given(limbo.getLocation()).willReturn(limboLocation);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
@@ -317,7 +317,7 @@ public class TeleportationServiceTest {
|
||||
LimboPlayer limbo = mock(LimboPlayer.class);
|
||||
Location limboLocation = mockLocation();
|
||||
given(limbo.getLocation()).willReturn(limboLocation);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
@@ -342,7 +342,7 @@ public class TeleportationServiceTest {
|
||||
LimboPlayer limbo = mock(LimboPlayer.class);
|
||||
Location location = mockLocation();
|
||||
given(limbo.getLocation()).willReturn(location);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
@@ -364,7 +364,7 @@ public class TeleportationServiceTest {
|
||||
LimboPlayer limbo = mock(LimboPlayer.class);
|
||||
Location location = mockLocation();
|
||||
given(limbo.getLocation()).willReturn(location);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
|
||||
Reference in New Issue
Block a user