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
@@ -1,10 +1,8 @@
package fr.xephi.authme;
import ch.jalu.configme.properties.Property;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import org.bukkit.entity.Player;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.io.File;
@@ -21,11 +19,8 @@ import java.nio.file.Paths;
import java.util.logging.Logger;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* AuthMe test utilities.
@@ -78,76 +73,6 @@ public final class TestHelper {
}
}
/**
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#runTaskAsynchronously} method.
* Note that calling this method expects that there be a runnable sent to the method and will fail
* otherwise.
*
* @param service The mock service
*/
public static void runInnerRunnable(BukkitService service) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(service).runTaskAsynchronously(captor.capture());
Runnable runnable = captor.getValue();
runnable.run();
}
/**
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#runTaskOptionallyAsync} method.
* Note that calling this method expects that there be a runnable sent to the method and will fail
* otherwise.
*
* @param service The mock service
*/
public static void runOptionallyAsyncTask(BukkitService service) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(service).runTaskOptionallyAsync(captor.capture());
Runnable runnable = captor.getValue();
runnable.run();
}
/**
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncDelayedTask(Runnable)}
* method. Note that calling this method expects that there be a runnable sent to the method and will fail
* otherwise.
*
* @param service The mock service
*/
public static void runSyncDelayedTask(BukkitService service) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(service).scheduleSyncDelayedTask(captor.capture());
Runnable runnable = captor.getValue();
runnable.run();
}
/**
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncDelayedTask(Runnable, long)}
* method. Note that calling this method expects that there be a runnable sent to the method and will fail
* otherwise.
*
* @param service The mock service
*/
public static void runSyncDelayedTaskWithDelay(BukkitService service) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(service).scheduleSyncDelayedTask(captor.capture(), anyLong());
Runnable runnable = captor.getValue();
runnable.run();
}
/**
* 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 setBukkitServiceToRunOptionallyAsyncTasks(BukkitService bukkitService) {
doAnswer(invocation -> {
Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
}).when(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class));
}
/**
* Assign the necessary fields on ConsoleLogger with mocks.
*