Guides · Engineering
Testing a Minecraft plugin with MockBukkit and JUnit
Start a fake server in a JUnit test, load your plugin, add players, run commands and advance ticks. A practical MockBukkit setup for Paper plugins.
Most plugins are tested the same way: build the jar, copy it into a test server, restart, join, type a few commands. It works, it takes minutes per change, and the parts you don't click through on that day ship untested. That's how a refactor of the arena code breaks /arena leave and nobody notices until a server owner does.
MockBukkit is a mock implementation of the Bukkit and Paper API. It starts a fake server inside a JUnit test, loads your plugin into it, and lets you add players, run commands and advance ticks in milliseconds. It doesn't replace a real server, but it catches the regressions that a real server only shows you after release.
Adding it to your build
MockBukkit is published on Maven Central under the group org.mockbukkit.mockbukkit, with one artifact per Minecraft version it implements, named mockbukkit-v<version>. Pick the one that matches the API you compile against, and the latest version number from Maven Central. MockBukkit's own setup also adds the Paper repository, for the Paper API.
With Gradle:
repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
}
dependencies {
testImplementation("org.mockbukkit.mockbukkit:mockbukkit-v1.21:<version>")
testImplementation("org.junit.jupiter:junit-jupiter:<version>")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
tasks.test {
useJUnitPlatform()
}
MockBukkit's examples use JUnit 5 (Jupiter), and that's what its docs recommend. With Maven it's the same coordinates with <scope>test</scope>.
The first test
Every test starts a server, loads your plugin, and stops the server afterwards so tests don't leak into each other:
import org.mockbukkit.mockbukkit.MockBukkit;
import org.mockbukkit.mockbukkit.ServerMock;
class ArenaPluginTest {
private ServerMock server;
private ArenaPlugin plugin;
@BeforeEach
void setUp() {
server = MockBukkit.mock();
plugin = MockBukkit.load(ArenaPlugin.class);
}
@AfterEach
void tearDown() {
MockBukkit.unmock();
}
@Test
void pluginEnables() {
assertTrue(plugin.isEnabled());
}
}
MockBukkit.load runs your onEnable, with your plugin.yml and config.yml from the jar's resources. If onEnable throws, the test fails right there, which on its own catches a surprising number of broken releases: a missing default in config.yml, a listener that needs a class that isn't there, a command missing from plugin.yml. Your plugin's main class can't be final.
Players and commands
Players are added to the fake server, and they record every message they receive:
@Test
void joinCommandPutsPlayerInQueue() {
PlayerMock player = server.addPlayer("Steve");
player.performCommand("arena join");
player.assertSaid("§aYou joined the queue (1/2)");
assertTrue(plugin.queue().contains(player.getUniqueId()));
}
assertSaid checks the next message the player got, and assertNoMoreSaid() checks there's nothing else. When you don't want to match the exact text, nextMessage() returns it so you can check part of it. server.setPlayers(10) adds ten players at once, which helps for anything that needs a full lobby.
Assert on your plugin's state as well as on messages. A message is the easiest thing to change in an update; the queue having the player in it is what actually matters.
Ticks and scheduled tasks
Countdowns, round timers and delayed tasks run on the scheduler, and MockBukkit's scheduler only moves when you tell it to:
@Test
void roundStartsAfterCountdown() {
server.addPlayer();
server.addPlayer();
plugin.arenas().get("default").tryStart();
server.getScheduler().performTicks(20L * 10); // 10 seconds
assertEquals(ArenaState.RUNNING, plugin.arenas().get("default").state());
}
performTicks runs every tick in order, as a real server would. That makes timing tests exact and instant: no Thread.sleep, no flaky waits.
What to test, and what not
MockBukkit is a mock, not a server. It implements a large part of the API, and when your code calls something it doesn't implement, it throws an UnimplementedOperationException, which makes JUnit skip the test instead of failing it. A skipped test isn't a passing one, so check the report.
That shapes what's worth testing with it:
- Your logic. Queues, arena states, permissions checks, cooldowns, economy math, command parsing. This is where most regressions are, and MockBukkit handles it well.
- Config handling. Load an old config.yml from
src/test/resourcesand check your plugin reads or migrates it correctly. Updating config.yml without resetting it - Not world generation, lighting, NMS or packets. Anything that depends on the real server's internals belongs on a test server.
A good split is to keep logic in plain classes that take what they need as parameters, and keep Bukkit calls at the edges. Plain classes can be tested without MockBukkit at all, and the tests that need it stay short.
Analytics in tests
If your plugin starts an analytics or metrics library in onEnable, check what it does when tests run, since every test calls onEnable again. You don't want your test runs counted as servers or sending network requests from CI.
With PluginAnalytics there's nothing to change. The SDK refuses to run when it hasn't been relocated, and in a test run your classes come from the build output, not the shaded jar, so PluginAnalytics.start logs a warning and analytics stays off. Every method then does nothing, so track, time and captureError calls in the code you test are safe. Don't set the pluginanalytics.dev system property in your test configuration: it skips that check and is meant for running the SDK unrelocated on purpose. SDK API reference
Run it on every push
Tests nobody runs don't catch anything. Add ./gradlew test (or mvn test) to a GitHub Actions workflow so every push and pull request runs them, and make the release build depend on them.
Tests catch what you thought to test. What breaks on real servers, with other plugins and configs you've never seen, shows up in the hours after a release instead. That's what error tracking and a look at your numbers after each update are for. Did your update break something?
More guides
Did your plugin update break something? How to know in the first hours
How to tell within hours whether a new version of your Spigot, Paper or proxy plugin introduced a bug: errors per version, affected installs, logs and lag.
4 min readEngineeringError tracking for Minecraft plugins: find the bug before the 1-star review
How Spigot and Paper plugin exceptions reach the console, why waiting for reports fails, and how to collect, group and fix errors from every server.
4 min read