Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 11, 2024
1 parent 638bbc9 commit 41a6ade
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.parkour.mocks.ServerMocks;

/**
* @author tastybento
Expand Down Expand Up @@ -96,6 +97,7 @@ public static void beforeClass() throws IllegalAccessException, InvocationTarget

@After
public void tearDown() throws IOException {
ServerMocks.unsetBukkitServer();
User.clearUsers();
Mockito.framework().clearInlineMocks();
deleteAll(new File("database"));
Expand All @@ -117,6 +119,7 @@ protected void deleteAll(File file) throws IOException {
*/
@Before
public void setUp() throws Exception {
Server server = ServerMocks.newServer();
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
Expand Down Expand Up @@ -158,7 +161,6 @@ public void setUp() throws Exception {

// Server
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
Server server = mock(Server.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/world/bentobox/parkour/ParkourManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.parkour.mocks.ServerMocks;
import world.bentobox.parkour.objects.ParkourData;

/**
Expand Down Expand Up @@ -99,6 +100,8 @@ public static void beforeClass() throws IllegalAccessException, InvocationTarget

@Before
public void setUp() {
ServerMocks.newServer();

when(addon.getPlugin()).thenReturn(plugin);
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Expand Down Expand Up @@ -150,6 +153,7 @@ public void setUp() {
*/
@After
public void tearDown() throws Exception {
ServerMocks.unsetBukkitServer();
deleteAll(new File("database"));
User.clearUsers();
Mockito.framework().clearInlineMocks();
Expand Down
21 changes: 17 additions & 4 deletions src/test/java/world/bentobox/parkour/SettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Server;
import org.bukkit.block.Biome;
import org.bukkit.entity.EntityType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import world.bentobox.bentobox.api.user.User;
import world.bentobox.parkour.mocks.ServerMocks;

/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(Bukkit.class)
public class SettingsTest {

private Settings s;
Expand All @@ -32,14 +42,17 @@ public class SettingsTest {
*/
@Before
public void setUp() throws Exception {
Server server = ServerMocks.newServer();
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getServer()).thenReturn(server);
s = new Settings();
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
public void tearDown() {
ServerMocks.unsetBukkitServer();
User.clearUsers();
Mockito.framework().clearInlineMocks();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
Expand All @@ -24,6 +25,7 @@
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -50,6 +52,7 @@
import world.bentobox.parkour.ParkourManager;
import world.bentobox.parkour.Settings;
import world.bentobox.parkour.gui.RankingsUI;
import world.bentobox.parkour.mocks.ServerMocks;

/**
* @author tastybento
Expand Down Expand Up @@ -96,6 +99,7 @@ public class ClearTopCommandTest {
*/
@Before
public void setUp() throws Exception {
ServerMocks.newServer();
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
world.bentobox.bentobox.Settings s = new world.bentobox.bentobox.Settings();
Expand Down Expand Up @@ -166,18 +170,22 @@ public void setUp() throws Exception {
// DUT
cmd = new ClearTopCommand(ac);
}

@After
public void tearDown() {
ServerMocks.unsetBukkitServer();
User.clearUsers();
Mockito.framework().clearInlineMocks();
}

/**
* Test method for {@link world.bentobox.parkour.commands.ClearTopCommand#ClearTopCommand(world.bentobox.bentobox.api.commands.CompositeCommand)}.
*/
@Test
public void testClearTopCommand() {
assertNonNull(cmd);
assertNotNull(cmd);
}

private void assertNonNull(ClearTopCommand cmd2) {
// TODO Auto-generated method stub

}
/**
* Test method for {@link world.bentobox.parkour.commands.ClearTopCommand#setup()}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package world.bentobox.parkour.listeners;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -9,6 +10,7 @@
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_MOCKS;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
Expand All @@ -32,6 +34,7 @@
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Player;
import org.bukkit.entity.Player.Spigot;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
Expand All @@ -46,11 +49,13 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

import net.md_5.bungee.api.chat.TextComponent;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.island.IslandEnterEvent;
import world.bentobox.bentobox.api.events.island.IslandExitEvent;
Expand Down Expand Up @@ -100,6 +105,8 @@ public class CourseRunnerListenerTest extends AbstractParkourTest {
private Block block;
@Mock
private User u;
@Mock
private Spigot spigot;

/**
* @throws java.lang.Exception
Expand All @@ -118,6 +125,7 @@ public void setUp() throws Exception {
when(player.hasPermission(anyString())).thenReturn(false);
when(player.getGameMode()).thenReturn(GameMode.SURVIVAL);
when(player.getServer()).thenReturn(server);
when(player.spigot()).thenReturn(spigot);
User.setPlugin(plugin);
user = User.getInstance(player);

Expand Down Expand Up @@ -414,7 +422,7 @@ public void testOnVisitorCommandQuittingParkour() {
public void testOnStartEndSet() {
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.PHYSICAL, null, block, BlockFace.DOWN);
crl.onStartEndSet(e);
verify(player).sendMessage("parkour.start");
checkSpigotMessage("parkour.start");
}

/**
Expand All @@ -425,7 +433,7 @@ public void testOnStartEndSetNoEnd() {
when(this.parkourManager.getEnd(island)).thenReturn(Optional.empty());
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.PHYSICAL, null, block, BlockFace.DOWN);
crl.onStartEndSet(e);
verify(player).sendMessage("parkour.set-the-end");
checkSpigotMessage("parkour.set-the-end");
}

/**
Expand Down Expand Up @@ -657,4 +665,32 @@ public void testOnTeleportToFlagActionVisitorsChorusFruit() {
verify(player, never()).setGameMode(GameMode.SURVIVAL);
}

/**
* Check that spigot sent the message
* @param message - message to check
*/
public void checkSpigotMessage(String expectedMessage) {
checkSpigotMessage(expectedMessage, 1);
}

public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) {
// Capture the argument passed to spigot().sendMessage(...) if messages are sent
ArgumentCaptor<TextComponent> captor = ArgumentCaptor.forClass(TextComponent.class);

// Verify that sendMessage() was called at least 0 times (capture any sent messages)
verify(spigot, atLeast(0)).sendMessage(captor.capture());

// Get all captured TextComponents
List<TextComponent> capturedMessages = captor.getAllValues();

// Count the number of occurrences of the expectedMessage in the captured messages
long actualOccurrences = capturedMessages.stream().map(component -> component.toLegacyText()) // Convert each TextComponent to plain text
.filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text
.count(); // Count how many times the expected message appears

// Assert that the number of occurrences matches the expectedOccurrences
assertEquals("Expected message occurrence mismatch: " + expectedMessage, expectedOccurrences,
actualOccurrences);
}

}
Loading

0 comments on commit 41a6ade

Please sign in to comment.