Skip to content

Lesson 12 of 13 · 16m

Testing Plugins: MockBukkit and a Real Server

Keep Bukkit types at the edges, MockBukkit for lifecycle tests, and run-paper for a real headless Paper instance.

Bukkit.getServer() is a static call into a running server, which makes a plugin the least unit-testable codebase you will own. The honest answer is a split: unit-test the parts that are plain Java, mock the Bukkit surface for lifecycle logic, and run integration checks against an actual Paper server for everything else.

The plain-Java majority

Most bugs live in your own classes — the claim-region math, the config migration, the message formatter. Those take a normal JUnit test; no server needed. The design rule that makes it possible is boring: keep Bukkit types at the edges. A ClaimService that takes coordinates and returns a Set is testable. One that takes Player and calls player.getWorld() inside the math is not. Pass primitives in, pass results out.

MockBukkit for the lifecycle

MockBukkit (io.github.seeseemelk:MockBukkit-v1.21) emulates enough of a Paper server for real tests:

class SudoToolsTest {
    private ServerMock server;
    private SudoToolsPlugin plugin;

    @BeforeEach
    void setUp() {
        server = MockBukkit.mock();
        plugin = MockBukkit.load(SudoToolsPlugin.class);
    }

    @AfterEach
    void tearDown() { MockBukkit.unmock(); }

    @Test
    void joinListenerWelcomes() {
        PlayerMock p = server.addPlayer();
        server.getPluginManager().callEvent(
            new PlayerJoinEvent(p, Component.text("joined")));
        p.assertSaid("Welcome");
    }
}

MockBukkit.mock() builds a ServerMock; MockBukkit.load(...) runs your plugin's real onEnable against it — including saveDefaultConfig, listener registration, and command wiring — and PlayerMock simulates joins, inventory interactions, block breaks (p.simulateBlockBreak(...)), and command sends, with assertions like assertSaid and nextMessage(). It covers listeners, commands, and lifecycle ordering — the class of bugs a config typo hides until production.

Know the limits before you rely on it: no real world ticking, no client, partial coverage of newer API surfaces. MockBukkit proves your code did the right thing given an event. It does not prove the server sends that event the way you assumed.

What to actually test

Order matters when time is short: (1) onEnable wiring — does the plugin enable cleanly with a fresh data folder; (2) command permission gates — op vs non-op vs console; (3) listener behavior on the events that cost money — a cancelled BlockBreakEvent in the wrong place is a grief vector, not a typo. Everything below that line is the same JUnit you write anywhere.

Integration against real Paper

The end-to-end check is a real server. xyz.jpenilla.run-paper (./gradlew runServer) boots Paper with your jar installed; in CI the same thing runs headless — download the Paper build through the api.papermc.io v2 endpoint, boot, wait for Done in latest.log, run a scripted scenario, grep the log for exceptions. That catches what mocks cannot: PluginClassLoader resolution, plugin.yml mistakes, API methods missing on your oldest supported version.

A useful smoke set on a headless server: plugin shows green in /plugins, /yourcommand replies, an op-only subcommand denies a fresh player, and the log holds zero stack traces through two restarts. Script it once — a five-line bash script is enough — and it runs on every commit for the rest of the plugin's life.

Field notes

  • onEnable and onDisable are the highest-value test targets; most production failures are lifecycle failures. MockBukkit's load() exercising them is worth the dependency alone.
  • server.execute("sudo", sender, "reload") drives your commands end-to-end inside the mock — test the parser, not just the executor method.
  • Test against the oldest Minecraft version you claim to support. NoSuchMethodError on an old server is a download-page lie.
  • Keep tests off live data: temp getDataFolder() paths, a throwaway SQLite file, never your dev server's world/.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.