Skip to content

Commit

Permalink
Added test class
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 13, 2024
1 parent cc6db10 commit c69ecb2
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<powermock.version>1.7.4</powermock.version>
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.20.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.0.0-SNAPSHOT</bentobox.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,25 @@ public void onEntitySpawn(CreatureSpawnEvent event)
this.addon.getPlugin().getIWM().getAddon(world);

if (!optionalAddon.isPresent() ||
!this.addon.getSettings().getDisabledGameModes().isEmpty() &&
(!this.addon.getSettings().getDisabledGameModes().isEmpty()
&&
this.addon.getSettings().getDisabledGameModes().contains(
optionalAddon.get().getDescription().getName()))
optionalAddon.get().getDescription().getName())))
{
// GameMode addon is not in enable list.
return;
}


if ((event.getEntityType().equals(EntityType.ZOMBIFIED_PIGLIN)
|| event.getEntityType().equals(EntityType.PIGLIN))
&&
this.addon.getPlugin().getIWM().isIslandNether(world))
&& this.addon.getPlugin().getIWM().isIslandNether(world))
{

// replace pigmen with blaze or wither

if (this.isSuitableNetherLocation(event.getLocation()))
{

if (this.spawningRandom.nextDouble() < this.addon.getSettings().getWitherSkeletonChance())
{
// oOo wither skeleton got lucky.
Expand Down Expand Up @@ -108,6 +109,7 @@ else if (event.getEntityType() == EntityType.ENDERMAN &&
}
else if (world.getEnvironment() == World.Environment.NORMAL && event.getEntity() instanceof Fish)
{

// Check biome
Biome biome = world.getBiome(
event.getLocation().getBlockX(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package world.bentobox.extramobs.listeners;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Optional;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Fish;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.AddonDescription;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.extramobs.ExtraMobsAddon;
import world.bentobox.extramobs.config.Settings;

@RunWith(PowerMockRunner.class)
public class MobsSpawnListenerTest {

@Mock
private ExtraMobsAddon addon;

@Mock
private CreatureSpawnEvent event;

@Mock
private World world;

private MobsSpawnListener listener;

private Settings settings;

@Mock
private BentoBox plugin;

@Mock
private IslandWorldManager iwm;

@Mock
private GameModeAddon gma;

@Mock
private Location location;

@Mock
private Block block;

@Before
public void setUp() {
settings = new Settings();
when(addon.getSettings()).thenReturn(settings);

when(addon.getPlugin()).thenReturn(plugin);

when(plugin.getIWM()).thenReturn(iwm);

when(iwm.getAddon(world)).thenReturn(Optional.of(gma));

when(iwm.isIslandEnd(world)).thenReturn(true);
when(iwm.isIslandNether(world)).thenReturn(true);

@NonNull
AddonDescription desc = new AddonDescription.Builder("main", "bskyblock", "1.0.0").build();

when(gma.getDescription()).thenReturn(desc);

// Location
when(location.getBlock()).thenReturn(block);
when(location.getWorld()).thenReturn(world);

when(block.getRelative(any())).thenReturn(block);

when(block.getType()).thenReturn(Material.STONE);

// Initialize mocks and the class to test
listener = new MobsSpawnListener(addon);
}

// Test case for natural spawning of Zombified Piglin in the Nether
@Test
public void testNaturalSpawnZombifiedPiglinNether() {
when(event.getEntityType()).thenReturn(EntityType.ZOMBIFIED_PIGLIN);
when(event.getSpawnReason()).thenReturn(CreatureSpawnEvent.SpawnReason.NATURAL);
when(event.getLocation()).thenReturn(location);
when(world.getEnvironment()).thenReturn(World.Environment.NETHER);
when(addon.getPlugin().getIWM().isIslandNether(world)).thenReturn(true);
settings.setWitherSkeletonChance(1.1); // Set so that it will always spawn
when(block.getType()).thenReturn(Material.NETHER_BRICKS);

listener.onEntitySpawn(event);

verify(event).setCancelled(true);
// Additional verifications can be added to check if the correct entity was spawned
}

// Test case for natural spawning of Enderman in the End
@Test
public void testNaturalSpawnEndermanEnd() {
when(event.getEntityType()).thenReturn(EntityType.ENDERMAN);
when(event.getSpawnReason()).thenReturn(CreatureSpawnEvent.SpawnReason.NATURAL);
when(event.getLocation()).thenReturn(location);
when(world.getEnvironment()).thenReturn(World.Environment.THE_END);
when(addon.getPlugin().getIWM().isIslandEnd(world)).thenReturn(true);
settings.setShulkerChance(1.1); // Set so that it will always spawn
when(block.getType()).thenReturn(Material.PURPUR_BLOCK);

listener.onEntitySpawn(event);

verify(event).setCancelled(true);
// Additional verifications can be added to check if the correct entity was spawned
}

// Test case for spawning of Fish in Deep Ocean biome
@Test
public void testFishSpawnDeepOcean() {
Fish fish = mock(Fish.class);
when(event.getEntity()).thenReturn(fish);
when(event.getEntityType()).thenReturn(EntityType.TROPICAL_FISH);
when(event.getSpawnReason()).thenReturn(CreatureSpawnEvent.SpawnReason.NATURAL);
when(event.getLocation()).thenReturn(location);
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(world.getBiome(anyInt(), anyInt(), anyInt())).thenReturn(Biome.DEEP_OCEAN);
settings.setGuardianChance(1.1); // Set so that it will always spawn
when(block.getType()).thenReturn(Material.WATER, Material.WATER, Material.WATER, Material.PRISMARINE);

listener.onEntitySpawn(event);

verify(event).setCancelled(true);
// Additional verifications for guardian spawning
}

// Test case for non-natural spawning
@Test
public void testNonNaturalSpawn() {
when(event.getSpawnReason()).thenReturn(CreatureSpawnEvent.SpawnReason.SPAWNER);

listener.onEntitySpawn(event);

verify(event, never()).isCancelled();
}

}

0 comments on commit c69ecb2

Please sign in to comment.