Skip to content

Commit

Permalink
Replaced mocking of JavaPlugin with mockPlugin method
Browse files Browse the repository at this point in the history
  • Loading branch information
fulminazzo committed Apr 12, 2024
1 parent efc4e74 commit 165712c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.angrybear.yagl.contents.ItemGUIContent;
import it.angrybear.yagl.guis.GUI;
import it.angrybear.yagl.items.Item;
import it.angrybear.yagl.utils.GUITestUtils;
import it.angrybear.yagl.viewers.PlayerOfflineException;
import it.fulminazzo.fulmicollection.objects.Refl;
import it.fulminazzo.jbukkit.BukkitUtils;
Expand All @@ -13,11 +14,9 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -94,10 +93,7 @@ void testOpenGUIForOfflinePlayer() {
}

private void openGUI(GUI gui) {
try (MockedStatic<JavaPlugin> ignored = mockStatic(JavaPlugin.class)) {
when(JavaPlugin.getProvidingPlugin(any())).thenAnswer(a -> mock(JavaPlugin.class));
gui.open(GUIManager.getViewer(this.player));
}
GUITestUtils.mockPlugin(p -> gui.open(GUIManager.getViewer(this.player)));
}

}
27 changes: 8 additions & 19 deletions gui/bukkit/src/test/java/it/angrybear/yagl/GUIManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.angrybear.yagl.guis.GUI;
import it.angrybear.yagl.items.Item;
import it.angrybear.yagl.utils.GUITestUtils;
import it.angrybear.yagl.viewers.Viewer;
import it.angrybear.yagl.wrappers.Sound;
import it.fulminazzo.jbukkit.BukkitUtils;
Expand All @@ -20,7 +21,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import java.util.HashMap;
import java.util.UUID;
Expand All @@ -34,13 +34,8 @@ class GUIManagerTest {
@Test
void testGetOpenGUIViewerUUID() {
BukkitUtils.setupServer();
try (MockedStatic<JavaPlugin> ignored = mockStatic(JavaPlugin.class)) {
JavaPlugin plugin = mock(JavaPlugin.class);
when(JavaPlugin.getProvidingPlugin(any())).thenAnswer(a -> plugin);
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));

assertFalse(GUIManager.getOpenGUIViewer(UUID.randomUUID()).isPresent(), "Should not be present");
}
GUITestUtils.mockPlugin(p ->
assertFalse(GUIManager.getOpenGUIViewer(UUID.randomUUID()).isPresent(), "Should not be present"));
}

@Nested
Expand Down Expand Up @@ -216,26 +211,20 @@ void testCloseEvent() {

@Test
void testDisableThisPlugin() {
try (MockedStatic<JavaPlugin> ignored = mockStatic(JavaPlugin.class)) {
JavaPlugin plugin = mock(JavaPlugin.class);
when(JavaPlugin.getProvidingPlugin(any())).thenAnswer(a -> plugin);

PluginDisableEvent event = new PluginDisableEvent(plugin);
GUITestUtils.mockPlugin(p -> {
PluginDisableEvent event = new PluginDisableEvent(p);
this.guiManager.on(event);
verify(this.player).closeInventory();
}
});
}

@Test
void testDisableNotThisPlugin() {
try (MockedStatic<JavaPlugin> ignored = mockStatic(JavaPlugin.class)) {
JavaPlugin plugin = mock(JavaPlugin.class);
when(JavaPlugin.getProvidingPlugin(any())).thenAnswer(a -> plugin);

GUITestUtils.mockPlugin(p -> {
PluginDisableEvent event = new PluginDisableEvent(mock(JavaPlugin.class));
this.guiManager.on(event);
verify(this.player, never()).closeInventory();
}
});
}

private @NotNull InventoryView getView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
import it.angrybear.yagl.contents.GUIContent;
import it.angrybear.yagl.contents.ItemGUIContent;
import it.angrybear.yagl.guis.GUI;
import it.angrybear.yagl.utils.GUITestUtils;
import it.angrybear.yagl.viewers.Viewer;
import it.fulminazzo.jbukkit.BukkitUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedStatic;

import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class BukkitCommandActionTest {
private static final String COMMAND = "/helloworld";
Expand Down Expand Up @@ -47,11 +46,7 @@ private static Object[][] guiActions() {
@ParameterizedTest
@MethodSource("guiActions")
void testGUICommandActions(Consumer<GUI> setupAction, BiConsumer<Viewer, GUI> runAction) {
try (MockedStatic<JavaPlugin> ignored = mockStatic(JavaPlugin.class)) {
JavaPlugin plugin = mock(JavaPlugin.class);
when(JavaPlugin.getProvidingPlugin(any())).thenAnswer(a -> plugin);
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));

GUITestUtils.mockPlugin(p -> {
Player player = BukkitUtils.addPlayer(UUID.randomUUID(), "Alex");
when(player.isOnline()).thenReturn(true);

Expand All @@ -60,7 +55,7 @@ void testGUICommandActions(Consumer<GUI> setupAction, BiConsumer<Viewer, GUI> ru
runAction.accept(GUIManager.getViewer(player), gui);

verify(Bukkit.getServer()).dispatchCommand(player, COMMAND);
}
});
}

}

0 comments on commit 165712c

Please sign in to comment.