diff --git a/gui/bukkit/src/test/java/it/angrybear/yagl/utils/GUITestUtils.java b/gui/bukkit/src/test/java/it/angrybear/yagl/utils/GUITestUtils.java new file mode 100644 index 00000000..d803baf3 --- /dev/null +++ b/gui/bukkit/src/test/java/it/angrybear/yagl/utils/GUITestUtils.java @@ -0,0 +1,34 @@ +package it.angrybear.yagl.utils; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.mockito.MockedStatic; + +import java.util.function.Consumer; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +/** + * A collection of utilities used to test the module + */ +public class GUITestUtils { + + /** + * Mocks the returned value of {@link JavaPlugin#getProvidingPlugin(Class)} and executes the given function. + * + * @param function the function + */ + public static void mockPlugin(final @NotNull Consumer function) { + 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)); + + function.accept(plugin); + } + } +}