Fix all the NPE's on tests when getting a task's id

This has been bugging me for far too long, I hated seeing all those
NPE's on test results.
This commit is contained in:
graywolf336 2015-05-27 10:36:56 -05:00
parent 9b638d6360
commit 2d11239ce4

View File

@ -39,6 +39,7 @@ import org.bukkit.plugin.PluginLogger;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import org.junit.Assert; import org.junit.Assert;
import org.mockito.Matchers; import org.mockito.Matchers;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
@ -68,6 +69,7 @@ public class TestInstanceCreator {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public boolean setup() { public boolean setup() {
r = new Random(); r = new Random();
try { try {
pluginDirectory.mkdirs(); pluginDirectory.mkdirs();
Assert.assertTrue(pluginDirectory.exists()); Assert.assertTrue(pluginDirectory.exists());
@ -176,42 +178,47 @@ public class TestInstanceCreator {
when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true); when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true);
// add mock scheduler // add mock scheduler
final BukkitTask bt = mock(BukkitTask.class);
when(bt.getTaskId()).thenReturn(r.nextInt());
when(bt.getOwner()).thenReturn(main);
when(bt.isSync()).thenReturn(false);
BukkitScheduler mockScheduler = mock(BukkitScheduler.class); BukkitScheduler mockScheduler = mock(BukkitScheduler.class);
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong())). when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong())).
thenAnswer(new Answer<Integer>() { thenAnswer(new Answer<BukkitTask>() {
public Integer answer(InvocationOnMock invocation) throws Throwable { public BukkitTask answer(InvocationOnMock invocation) throws Throwable {
Runnable arg; Runnable arg;
try { try {
arg = (Runnable) invocation.getArguments()[1]; arg = (Runnable) invocation.getArguments()[1];
} catch (Exception e) { } catch (Exception e) {
return null; return bt;
} }
arg.run(); arg.run();
return null; return bt;
}}); }});
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class))). when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class))).
thenAnswer(new Answer<Integer>() { thenAnswer(new Answer<BukkitTask>() {
public Integer answer(InvocationOnMock invocation) throws Throwable { public BukkitTask answer(InvocationOnMock invocation) throws Throwable {
Runnable arg; Runnable arg;
try { try {
arg = (Runnable) invocation.getArguments()[1]; arg = (Runnable) invocation.getArguments()[1];
} catch (Exception e) { } catch (Exception e) {
return null; return bt;
} }
arg.run(); arg.run();
return null; return bt;
}}); }});
when(mockScheduler.runTaskTimerAsynchronously(any(Plugin.class), any(Runnable.class), anyLong(), anyLong())). when(mockScheduler.runTaskTimerAsynchronously(any(Plugin.class), any(Runnable.class), anyLong(), anyLong())).
thenAnswer(new Answer<Integer>() { thenAnswer(new Answer<BukkitTask>() {
public Integer answer(InvocationOnMock invocation) throws Throwable { public BukkitTask answer(InvocationOnMock invocation) throws Throwable {
Runnable arg; Runnable arg;
try { try {
arg = (Runnable) invocation.getArguments()[1]; arg = (Runnable) invocation.getArguments()[1];
} catch (Exception e) { } catch (Exception e) {
return null; return bt;
} }
arg.run(); arg.run();
return null; return bt;
}}); }});
when(mockServer.getScheduler()).thenReturn(mockScheduler); when(mockServer.getScheduler()).thenReturn(mockScheduler);