Fix tests

This commit is contained in:
tastybento 2024-10-26 16:44:46 -07:00
parent 68f2a0f59f
commit 41a2484c9d

View File

@ -4,10 +4,10 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -25,6 +25,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.junit.After; import org.junit.After;
@ -41,6 +42,7 @@ import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.island.IslandDeletedEvent; import world.bentobox.bentobox.api.events.island.IslandDeletedEvent;
@ -80,12 +82,19 @@ public class AdminPurgeCommandTest {
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private @NonNull Location location; private @NonNull Location location;
@Mock
private BukkitScheduler scheduler;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
// Mock the method to immediately run the Runnable
when(scheduler.runTaskLater(eq(plugin), any(Runnable.class), anyLong())).thenAnswer(invocation -> {
Runnable task = invocation.getArgument(1);
task.run(); // Immediately run the Runnable
return null; // or return a mock of the Task if needed
});
when(Bukkit.getScheduler()).thenReturn(scheduler);
// Set up plugin // Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
@ -118,6 +127,10 @@ public class AdminPurgeCommandTest {
when(plugin.getPlayers()).thenReturn(pm); when(plugin.getPlayers()).thenReturn(pm);
when(pm.getName(any())).thenReturn("name"); when(pm.getName(any())).thenReturn("name");
Settings settings = new Settings();
// Settings
when(plugin.getSettings()).thenReturn(settings);
// Command // Command
apc = new AdminPurgeCommand(ac); apc = new AdminPurgeCommand(ac);
} }
@ -315,7 +328,7 @@ public class AdminPurgeCommandTest {
testExecuteUserStringListOfStringIslandsFound(); testExecuteUserStringListOfStringIslandsFound();
assertTrue(apc.execute(user, "", Collections.singletonList("confirm"))); assertTrue(apc.execute(user, "", Collections.singletonList("confirm")));
verify(im).deleteIsland(eq(island), eq(true), eq(null)); verify(im).deleteIsland(eq(island), eq(true), eq(null));
verify(plugin, times(4)).log(any()); verify(plugin).log(any());
verify(user).sendMessage(eq("commands.admin.purge.see-console-for-status"), eq("[label]"), eq("bsb")); verify(user).sendMessage(eq("commands.admin.purge.see-console-for-status"), eq("[label]"), eq("bsb"));
} }