- 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
85 lines
3.1 KiB
Java
85 lines
3.1 KiB
Java
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());
|
|
}
|
|
}
|