Package be.seeseemelk.mockbukkit
Class MockBukkit
java.lang.Object
be.seeseemelk.mockbukkit.MockBukkit
Handles mocking the
Bukkit
server, along with containing some handy utility methods.-
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull MockPlugin
Creates a mock instance of aJavaPlugin
implementation.static @NotNull MockPlugin
createMockPlugin
(@NotNull String pluginName) Creates a mock instance of aJavaPlugin
implementation and gives you a chance to name the plugin.static void
Throws an IllegalStateException when not mocked.static @Nullable ServerMock
getMock()
Get the mock server instance.static @Nullable ServerMock
Get the mock server instance.static boolean
isMocked()
Checks if Bukkit is being mocked.static <T extends JavaPlugin>
TLoads and enables a plugin for mocking.static <T extends JavaPlugin>
TLoads and enables a plugin for mocking.static void
Loads a plugin from a jar.static void
Loads a plugin from a jar.static <T extends JavaPlugin>
TloadSimple
(@NotNull Class<T> plugin, Object @NotNull ... parameters) Loads and enables a plugin for mocking.static <T extends JavaPlugin>
TLoads and enables a plugin for mocking.static <T extends JavaPlugin>
TloadWith
(@NotNull Class<T> plugin, @NotNull InputStream descriptionInput, Object... parameters) Loads and enables a plugin for mocking.static <T extends JavaPlugin>
TloadWith
(@NotNull Class<T> plugin, @NotNull PluginDescriptionFile descriptionFile, Object @NotNull ... parameters) Loads and enables a plugin for mocking.static <T extends JavaPlugin>
TLoads and enables a plugin for mocking.static @NotNull ServerMock
mock()
Start mocking theBukkit
singleton.static <T extends ServerMock>
Tmock
(T serverMockImplementation) Start mocking theBukkit
singleton.protected static void
Sets the global server singleton inBukkit
back to zero.static void
unmock()
Unload all loaded plugins.
-
Method Details
-
setServerInstanceToNull
protected static void setServerInstanceToNull()Sets the global server singleton inBukkit
back to zero. -
mock
Start mocking theBukkit
singleton. Also returns theServerMock
that was created for ease of use.- Returns:
- The created
ServerMock
.
-
mock
Start mocking theBukkit
singleton. You can pass your own implementation of theServerMock
instance. The instance you passed is returned.- Type Parameters:
T
- The mock implementation to use.- Parameters:
serverMockImplementation
- your customServerMock
implementation.- Returns:
- The provided
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
Get the mock server instance.- Returns:
- The
ServerMock
instance ornull
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
Loads a plugin from a jar.- Parameters:
path
- Path to the jar.
-
loadJar
Loads a plugin from a jar.- Parameters:
jarFile
- Path to the jar.- Throws:
InvalidPluginException
- If an exception occurred while loading a plugin.
-
load
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 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 JavaPlugin> T loadWith(@NotNull @NotNull Class<T> plugin, @NotNull @NotNull PluginDescriptionFile descriptionFile, Object @NotNull ... parameters) Loads and enables a plugin for mocking. It receives theplugin.yml
to use as an extraInputStream
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 ofplugin.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 JavaPlugin> T loadWith(@NotNull @NotNull Class<T> plugin, @NotNull @NotNull InputStream descriptionInput, Object... parameters) Loads and enables a plugin for mocking. It receives theplugin.yml
to use as an extraFile
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 ofplugin.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 JavaPlugin> T loadWith(@NotNull @NotNull Class<T> plugin, @NotNull @NotNull File descriptionFile, Object... parameters) Loads and enables a plugin for mocking. It receives theplugin.yml
to use as an extraFile
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 ofplugin.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 JavaPlugin> T loadWith(@NotNull @NotNull Class<T> plugin, String descriptionFileName, Object... parameters) Loads and enables a plugin for mocking. It receives theplugin.yml
to use as a resource in the default package from an extraString
argument.- Type Parameters:
T
- The plugin's main class to load.- Parameters:
plugin
- The plugin to load for mocking.descriptionFileName
- The name of theplugin.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 JavaPlugin> T loadSimple(@NotNull @NotNull Class<T> plugin, Object @NotNull ... parameters) Loads and enables a plugin for mocking. It will not load theplugin.yml
file, but rather it will use a mock one. This can be useful in certain multi-project plugins where one cannot always access theplugin.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
Creates a mock instance of aJavaPlugin
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
Creates a mock instance of aJavaPlugin
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.
-
ensureMocking
public static void ensureMocking()Throws an IllegalStateException when not mocked.
-