Class MockBukkit

java.lang.Object
be.seeseemelk.mockbukkit.MockBukkit

public class MockBukkit extends Object
Handles mocking the Bukkit server, along with containing some handy utility methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static @NotNull MockPlugin
    Creates a mock instance of a JavaPlugin implementation.
    static @NotNull MockPlugin
    createMockPlugin(@NotNull String pluginName)
    Creates a mock instance of a JavaPlugin implementation and gives you a chance to name the plugin.
    static @NotNull MockPlugin
    createMockPlugin(@NotNull String pluginName, @NotNull String pluginVersion)
    Creates a mock instance of a JavaPlugin implementation and gives you a chance to name the plugin.
    static void
    Throws an IllegalStateException when not mocked.
    static @Nullable ServerMock
    Get the mock server instance.
    static @Nullable ServerMock
    Get the mock server instance.
    static boolean
    Checks if Bukkit is being mocked.
    static <T extends org.bukkit.plugin.java.JavaPlugin>
    T
    load(@NotNull Class<T> plugin)
    Loads and enables a plugin for mocking.
    static <T extends org.bukkit.plugin.java.JavaPlugin>
    T
    load(@NotNull Class<T> plugin, Object @NotNull ... parameters)
    Loads and enables a plugin for mocking.
    static void
    loadJar(@NotNull File jarFile)
    Loads a plugin from a jar.
    static void
    loadJar(@NotNull String path)
    Loads a plugin from a jar.
    static <T extends org.bukkit.plugin.java.JavaPlugin>
    T
    loadSimple(@NotNull Class<T> plugin, Object @NotNull ... parameters)
    Loads and enables a plugin for mocking.
    static <T extends org.bukkit.plugin.java.JavaPlugin>
    T
    loadWith(@NotNull Class<T> plugin, @NotNull File descriptionFile, Object... parameters)
    Loads and enables a plugin for mocking.
    static <T extends org.bukkit.plugin.java.JavaPlugin>
    T
    loadWith(@NotNull Class<T> plugin, @NotNull InputStream descriptionInput, Object... parameters)
    Loads and enables a plugin for mocking.
    static <T extends org.bukkit.plugin.java.JavaPlugin>
    T
    loadWith(@NotNull Class<T> plugin, String descriptionFileName, Object... parameters)
    Loads and enables a plugin for mocking.
    static <T extends org.bukkit.plugin.java.JavaPlugin>
    T
    loadWith(@NotNull Class<T> plugin, @NotNull org.bukkit.plugin.PluginDescriptionFile descriptionFile, Object @NotNull ... parameters)
    Loads and enables a plugin for mocking.
    static @NotNull ServerMock
    Start mocking the Bukkit singleton.
    static <T extends ServerMock>
    T
    mock(T serverMockImplementation)
    Start mocking the Bukkit singleton.
    protected static void
    Sets the global server singleton in Bukkit back to zero.
    static void
    Unload all loaded plugins.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • setServerInstanceToNull

      protected static void setServerInstanceToNull()
      Sets the global server singleton in Bukkit back to zero.
    • mock

      @NotNull public static @NotNull ServerMock mock()
      Start mocking the Bukkit singleton. Also returns the ServerMock that was created for ease of use.
      Returns:
      The created ServerMock.
    • mock

      @NotNull public static <T extends ServerMock> T mock(@NotNull T serverMockImplementation)
      Start mocking the Bukkit singleton. You can pass your own implementation of the ServerMock instance. The instance you passed is returned.
      Type Parameters:
      T - The mock implementation to use.
      Parameters:
      serverMockImplementation - your custom ServerMock implementation.
      Returns:
      The provided ServerMock.
    • getOrCreateMock

      @Nullable public static @Nullable ServerMock getOrCreateMock()
      Get the mock server instance. If no instance exists one will be created. Otherwise existing one is returned
      Returns:
      The ServerMock instance.
    • getMock

      @Nullable public static @Nullable ServerMock getMock()
      Get the mock server instance.
      Returns:
      The ServerMock instance or null if none is set up yet.
    • isMocked

      public static boolean isMocked()
      Checks if Bukkit is being mocked.
      Returns:
      true if Bukkit is being mocked, false if it is not.
    • loadJar

      public static void loadJar(@NotNull @NotNull String path)
      Loads a plugin from a jar.
      Parameters:
      path - Path to the jar.
    • loadJar

      public static void loadJar(@NotNull @NotNull File jarFile) throws org.bukkit.plugin.InvalidPluginException
      Loads a plugin from a jar.
      Parameters:
      jarFile - Path to the jar.
      Throws:
      org.bukkit.plugin.InvalidPluginException - If an exception occurred while loading a plugin.
    • load

      @NotNull public static <T extends org.bukkit.plugin.java.JavaPlugin> T load(@NotNull @NotNull Class<T> plugin)
      Loads and enables a plugin for mocking.
      Type Parameters:
      T - The plugin's main class to load.
      Parameters:
      plugin - The plugin to load for mocking.
      Returns:
      An instance of the plugin's main class.
    • load

      @NotNull public static <T extends org.bukkit.plugin.java.JavaPlugin> T load(@NotNull @NotNull Class<T> plugin, Object @NotNull ... parameters)
      Loads and enables a plugin for mocking.
      Type Parameters:
      T - The plugin's main class to load.
      Parameters:
      plugin - The plugin to load for mocking.
      parameters - Extra parameters to pass on to the plugin constructor.
      Returns:
      An instance of the plugin's main class.
    • loadWith

      @NotNull public static <T extends org.bukkit.plugin.java.JavaPlugin> T loadWith(@NotNull @NotNull Class<T> plugin, @NotNull @NotNull org.bukkit.plugin.PluginDescriptionFile descriptionFile, Object @NotNull ... parameters)
      Loads and enables a plugin for mocking. It receives the plugin.yml to use as an extra InputStream argument.
      Type Parameters:
      T - The plugin's main class to load.
      Parameters:
      plugin - The plugin to load for mocking.
      descriptionFile - The plugin description file to use instead of plugin.yml.
      parameters - Extra parameters to pass on to the plugin constructor.
      Returns:
      An instance of the plugin's main class.
    • loadWith

      @NotNull public static <T extends org.bukkit.plugin.java.JavaPlugin> T loadWith(@NotNull @NotNull Class<T> plugin, @NotNull @NotNull InputStream descriptionInput, Object... parameters)
      Loads and enables a plugin for mocking. It receives the plugin.yml to use as an extra File argument.
      Type Parameters:
      T - The plugin's main class to load.
      Parameters:
      plugin - The plugin to load for mocking.
      descriptionInput - The input stream to use instead of plugin.yml.
      parameters - Extra parameters to pass on to the plugin constructor.
      Returns:
      An instance of the plugin's main class.
    • loadWith

      @NotNull public static <T extends org.bukkit.plugin.java.JavaPlugin> T loadWith(@NotNull @NotNull Class<T> plugin, @NotNull @NotNull File descriptionFile, Object... parameters)
      Loads and enables a plugin for mocking. It receives the plugin.yml to use as an extra File argument.
      Type Parameters:
      T - The plugin's main class to load.
      Parameters:
      plugin - The plugin to load for mocking.
      descriptionFile - The file to use instead of plugin.yml.
      parameters - Extra parameters to pass on to the plugin constructor.
      Returns:
      An instance of the plugin's main class.
    • loadWith

      @NotNull public static <T extends org.bukkit.plugin.java.JavaPlugin> T loadWith(@NotNull @NotNull Class<T> plugin, String descriptionFileName, Object... parameters)
      Loads and enables a plugin for mocking. It receives the plugin.yml to use as a resource in the default package from an extra String argument.
      Type Parameters:
      T - The plugin's main class to load.
      Parameters:
      plugin - The plugin to load for mocking.
      descriptionFileName - The name of the plugin.yml file as a system resource.
      parameters - Extra parameters to pass on to the plugin constructor.
      Returns:
      An instance of the plugin's main class.
    • loadSimple

      @NotNull public static <T extends org.bukkit.plugin.java.JavaPlugin> T loadSimple(@NotNull @NotNull Class<T> plugin, Object @NotNull ... parameters)
      Loads and enables a plugin for mocking. It will not load the plugin.yml file, but rather it will use a mock one. This can be useful in certain multi-project plugins where one cannot always access the plugin.yml file easily during testing.
      Type Parameters:
      T - The plugin's main class to load.
      Parameters:
      plugin - The plugin to load for mocking.
      parameters - Extra parameters to pass on to the plugin constructor.
      Returns:
      An instance of the plugin's main class.
    • unmock

      public static void unmock()
      Unload all loaded plugins.
    • createMockPlugin

      @NotNull public static @NotNull MockPlugin createMockPlugin()
      Creates a mock instance of a JavaPlugin implementation. This plugin offers no functionality, but it does allow a plugin that might enable and disable other plugins to be tested.
      Returns:
      An instance of a mock plugin.
    • createMockPlugin

      @NotNull public static @NotNull MockPlugin createMockPlugin(@NotNull @NotNull String pluginName)
      Creates a mock instance of a JavaPlugin implementation and gives you a chance to name the plugin. This plugin offers no functionality, but it does allow a plugin that might enable and disable other plugins to be tested.
      Parameters:
      pluginName - A name of a new plugin.
      Returns:
      An instance of a mock plugin.
    • createMockPlugin

      @NotNull public static @NotNull MockPlugin createMockPlugin(@NotNull @NotNull String pluginName, @NotNull @NotNull String pluginVersion)
      Creates a mock instance of a JavaPlugin implementation and gives you a chance to name the plugin. This plugin offers no functionality, but it does allow a plugin that might enable and disable other plugins to be tested.
      Parameters:
      pluginName - A name of a new plugin.
      pluginVersion - The version of the new plugin.
      Returns:
      An instance of a mock plugin.
    • ensureMocking

      public static void ensureMocking()
      Throws an IllegalStateException when not mocked.