Fix for random test failures.

This commit is contained in:
tastybento 2022-03-12 19:12:13 -08:00
parent 945bfa66ee
commit 546cf2c9b9
2 changed files with 19 additions and 9 deletions

View File

@ -21,10 +21,12 @@ import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.junit.After; import org.junit.After;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -57,6 +59,7 @@ import world.bentobox.bentobox.util.Util;
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class)
public abstract class AbstractCommonSetup { public abstract class AbstractCommonSetup {
protected UUID uuid = UUID.randomUUID(); protected UUID uuid = UUID.randomUUID();
@ -136,7 +139,7 @@ public abstract class AbstractCommonSetup {
// Island - nothing is allowed by default // Island - nothing is allowed by default
when(island.isAllowed(any())).thenReturn(false); when(island.isAllowed(any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false); when(island.isAllowed(any(User.class), any())).thenReturn(false);
when(island.getOwner()).thenReturn(uuid); when(island.getOwner()).thenReturn(uuid);
when(island.getMemberSet()).thenReturn(ImmutableSet.of(uuid)); when(island.getMemberSet()).thenReturn(ImmutableSet.of(uuid));

View File

@ -69,6 +69,10 @@ public class EntityInteractListenerTest extends AbstractCommonSetup {
position = new Vector(10,10,10); position = new Vector(10,10,10);
when(inv.getItemInMainHand()).thenReturn(new ItemStack(Material.NAME_TAG)); when(inv.getItemInMainHand()).thenReturn(new ItemStack(Material.NAME_TAG));
// Initialize the Flags class. This is a workaround to prevent weird errors when mocking
// I think it's because the flag class needs to be initialized before use in argument matchers
Flags.TRADING.setDefaultSetting(false);
// Class under test // Class under test
eil = new EntityInteractListener(); eil = new EntityInteractListener();
} }
@ -91,7 +95,7 @@ public class EntityInteractListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnPlayerInteractAtEntityArmorStandAllowed() { public void testOnPlayerInteractAtEntityArmorStandAllowed() {
when(island.isAllowed(any(), any())).thenReturn(true); when(island.isAllowed(any(User.class), any())).thenReturn(true);
clickedEntity = mock(ArmorStand.class); clickedEntity = mock(ArmorStand.class);
when(clickedEntity.getLocation()).thenReturn(location); when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractAtEntityEvent e = new PlayerInteractAtEntityEvent(player, clickedEntity, position, hand); PlayerInteractAtEntityEvent e = new PlayerInteractAtEntityEvent(player, clickedEntity, position, hand);
@ -118,7 +122,7 @@ public class EntityInteractListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnPlayerInteractEntityHorseAllowed() { public void testOnPlayerInteractEntityHorseAllowed() {
when(island.isAllowed(any(), any())).thenReturn(true); when(island.isAllowed(any(User.class), any())).thenReturn(true);
clickedEntity = mock(Horse.class); clickedEntity = mock(Horse.class);
when(clickedEntity.getLocation()).thenReturn(location); when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand); PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand);
@ -145,7 +149,7 @@ public class EntityInteractListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnPlayerInteractEntityMinecartAllowed() { public void testOnPlayerInteractEntityMinecartAllowed() {
when(island.isAllowed(any(), any())).thenReturn(true); when(island.isAllowed(any(User.class), any())).thenReturn(true);
clickedEntity = mock(RideableMinecart.class); clickedEntity = mock(RideableMinecart.class);
when(clickedEntity.getLocation()).thenReturn(location); when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand); PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand);
@ -172,7 +176,7 @@ public class EntityInteractListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnPlayerInteractEntityBoatAllowed() { public void testOnPlayerInteractEntityBoatAllowed() {
when(island.isAllowed(any(), any())).thenReturn(true); when(island.isAllowed(any(User.class), any())).thenReturn(true);
clickedEntity = mock(Boat.class); clickedEntity = mock(Boat.class);
when(clickedEntity.getLocation()).thenReturn(location); when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand); PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand);
@ -199,7 +203,7 @@ public class EntityInteractListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnPlayerInteractAtEntityVillagerAllowed() { public void testOnPlayerInteractAtEntityVillagerAllowed() {
when(island.isAllowed(any(), any())).thenReturn(true); when(island.isAllowed(any(User.class), any())).thenReturn(true);
clickedEntity = mock(Villager.class); clickedEntity = mock(Villager.class);
when(clickedEntity.getLocation()).thenReturn(location); when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand); PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand);
@ -212,9 +216,10 @@ public class EntityInteractListenerTest extends AbstractCommonSetup {
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}. * Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}.
*/ */
@Test @Test
public void testOnPlayerInteractEntityNamingVillagerAllowedNoTrading() { public void testOnPlayerInteractEntityNamingVillagerAllowedNoTrading() {
when(island.isAllowed(any(), eq(Flags.TRADING))).thenReturn(false); when(island.isAllowed(any(User.class), eq(Flags.TRADING))).thenReturn(false);
when(island.isAllowed(any(), eq(Flags.NAME_TAG))).thenReturn(true); when(island.isAllowed(any(User.class), eq(Flags.NAME_TAG))).thenReturn(true);
clickedEntity = mock(Villager.class); clickedEntity = mock(Villager.class);
when(clickedEntity.getLocation()).thenReturn(location); when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand); PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand);
@ -271,8 +276,9 @@ public class EntityInteractListenerTest extends AbstractCommonSetup {
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}. * Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}.
*/ */
@Test @Test
public void testOnPlayerInteractEntityNamingWanderingTraderAllowedNoTrading() { public void testOnPlayerInteractEntityNamingWanderingTraderAllowedNoTrading() {
when(island.isAllowed(any(User.class), when(island.isAllowed(any(),
eq(Flags.TRADING))).thenReturn(false); eq(Flags.TRADING))).thenReturn(false);
when(island.isAllowed(any(User.class), when(island.isAllowed(any(User.class),
eq(Flags.NAME_TAG))).thenReturn(true); eq(Flags.NAME_TAG))).thenReturn(true);
@ -289,6 +295,7 @@ public class EntityInteractListenerTest extends AbstractCommonSetup {
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}. * Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}.
*/ */
@Test @Test
public void testOnPlayerInteractEntityNamingWanderingTraderAllowedTradingNoNaming() { public void testOnPlayerInteractEntityNamingWanderingTraderAllowedTradingNoNaming() {
when(island.isAllowed(any(User.class), eq(Flags.TRADING))).thenReturn(true); when(island.isAllowed(any(User.class), eq(Flags.TRADING))).thenReturn(true);
when(island.isAllowed(any(User.class), eq(Flags.NAME_TAG))).thenReturn(false); when(island.isAllowed(any(User.class), eq(Flags.NAME_TAG))).thenReturn(false);