From 165712c47957efc585b2633fab9a86ac45a03e13 Mon Sep 17 00:00:00 2001 From: Fulminazzo Date: Sat, 13 Apr 2024 00:53:10 +0200 Subject: [PATCH] Replaced mocking of JavaPlugin with mockPlugin method --- .../it/angrybear/yagl/GUIAdapterTest.java | 8 ++---- .../it/angrybear/yagl/GUIManagerTest.java | 27 ++++++------------- .../yagl/actions/BukkitCommandActionTest.java | 15 ++++------- 3 files changed, 15 insertions(+), 35 deletions(-) diff --git a/gui/bukkit/src/test/java/it/angrybear/yagl/GUIAdapterTest.java b/gui/bukkit/src/test/java/it/angrybear/yagl/GUIAdapterTest.java index 2cb2cbca..c87b637c 100644 --- a/gui/bukkit/src/test/java/it/angrybear/yagl/GUIAdapterTest.java +++ b/gui/bukkit/src/test/java/it/angrybear/yagl/GUIAdapterTest.java @@ -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; @@ -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; @@ -94,10 +93,7 @@ void testOpenGUIForOfflinePlayer() { } private void openGUI(GUI gui) { - try (MockedStatic 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))); } } \ No newline at end of file diff --git a/gui/bukkit/src/test/java/it/angrybear/yagl/GUIManagerTest.java b/gui/bukkit/src/test/java/it/angrybear/yagl/GUIManagerTest.java index 3d50fd35..2d37fef1 100644 --- a/gui/bukkit/src/test/java/it/angrybear/yagl/GUIManagerTest.java +++ b/gui/bukkit/src/test/java/it/angrybear/yagl/GUIManagerTest.java @@ -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; @@ -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; @@ -34,13 +34,8 @@ class GUIManagerTest { @Test void testGetOpenGUIViewerUUID() { BukkitUtils.setupServer(); - try (MockedStatic 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 @@ -216,26 +211,20 @@ void testCloseEvent() { @Test void testDisableThisPlugin() { - try (MockedStatic 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 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() { diff --git a/gui/bukkit/src/test/java/it/angrybear/yagl/actions/BukkitCommandActionTest.java b/gui/bukkit/src/test/java/it/angrybear/yagl/actions/BukkitCommandActionTest.java index 2a651ab6..7171924f 100644 --- a/gui/bukkit/src/test/java/it/angrybear/yagl/actions/BukkitCommandActionTest.java +++ b/gui/bukkit/src/test/java/it/angrybear/yagl/actions/BukkitCommandActionTest.java @@ -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"; @@ -47,11 +46,7 @@ private static Object[][] guiActions() { @ParameterizedTest @MethodSource("guiActions") void testGUICommandActions(Consumer setupAction, BiConsumer runAction) { - try (MockedStatic 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); @@ -60,7 +55,7 @@ void testGUICommandActions(Consumer setupAction, BiConsumer ru runAction.accept(GUIManager.getViewer(player), gui); verify(Bukkit.getServer()).dispatchCommand(player, COMMAND); - } + }); } }