First Tests
The First Test
In order to start testing, you will need a directory to contain your tests.
This directory will most often be in src/test
, next to src/main
.
For instance, if you have a class at src/main/com/github/username/MyPlugin.java
,
you would probably put unit tests for this class at
src/test/com/github/username/MyPluginTests.java
.
Note
The name of the test class does not matter, but it is common to name it
<ClassName>Tests
or <ClassName>Test
.
Your Main Class can’t be final
. If you use Kotlin, you main class needs to be an open
class.
Creating the Test Class
Once your directories are set up, you can create unit tests like this
1public class MyPluginTests {
2 private ServerMock server;
3 private MyPlugin plugin;
4
5 @BeforeEach
6 public void setUp() {
7 // Start the mock server
8 server = MockBukkit.mock();
9 // Load your plugin
10 plugin = MockBukkit.load(MyPlugin.class);
11 }
12
13 @AfterEach
14 public void tearDown() {
15 // Stop the mock server
16 MockBukkit.unmock();
17 }
18
19 @Test
20 public void thisTestWillFail() {
21 // Perform your test
22 }
23}
UnimplementedOperationException
Sometimes your code may use a method that is not yet implemented in MockBukkit.
When this happens MockBukkit will, instead of returning placeholder values, throw
an UnimplementedOperationException
.
These exception extends TestAbortedException
and will cause the test to be skipped.
These exceptions should just be ignored, though pull requests that add functionality to MockBukkit are always welcome!
As an alternative you can always extend ServerMock
and implement those missing methods.
Simply pass your custom implementation of ServerMock to the MockBukkit.mock(…) method.
MyCustomServerMock server = MockBukkit.mock(new MyCustomServerMock());