Skip to content

Commit 099228d

Browse files
committed
Improve test setup to support Bukkit
1 parent afcf257 commit 099228d

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,11 @@
277277
<version>4.11.0</version>
278278
<scope>test</scope>
279279
</dependency>
280+
<dependency>
281+
<groupId>org.mockito</groupId>
282+
<artifactId>mockito-inline</artifactId>
283+
<version>4.11.0</version>
284+
<scope>test</scope>
285+
</dependency>
280286
</dependencies>
281287
</project>

src/main/java/net/clementraynaud/skoice/util/ThreadUtil.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ private ThreadUtil() {
2929
}
3030

3131
public static void ensureNotMainThread(boolean disablingBypass) {
32-
try {
33-
if (Bukkit.isPrimaryThread() && (!disablingBypass || Bukkit.getPluginManager().isPluginEnabled("Skoice"))) {
34-
Exception exception = new IllegalStateException("This method should not be called from the main thread.");
35-
exception.printStackTrace();
36-
Skoice.analyticManager().getBugsnag().notify(exception, Severity.WARNING);
37-
}
38-
} catch (NullPointerException ignored) {
32+
if (Bukkit.isPrimaryThread() && (!disablingBypass || Bukkit.getPluginManager().isPluginEnabled("Skoice"))) {
33+
Exception exception = new IllegalStateException("This method should not be called from the main thread.");
34+
exception.printStackTrace();
35+
Skoice.analyticManager().getBugsnag().notify(exception, Severity.WARNING);
3936
}
4037
}
4138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.clementraynaud.skoice;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.plugin.PluginManager;
5+
import org.junit.jupiter.api.AfterAll;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.mockito.MockedStatic;
8+
9+
import static org.mockito.Mockito.mock;
10+
import static org.mockito.Mockito.mockStatic;
11+
import static org.mockito.Mockito.when;
12+
13+
public class BukkitTestSetup {
14+
15+
private static MockedStatic<Bukkit> mockedBukkit;
16+
17+
@BeforeAll
18+
static void setUpAll() {
19+
BukkitTestSetup.mockedBukkit = mockStatic(Bukkit.class);
20+
when(Bukkit.isPrimaryThread()).thenReturn(false);
21+
PluginManager mockPluginManager = mock(PluginManager.class);
22+
when(Bukkit.getPluginManager()).thenReturn(mockPluginManager);
23+
when(mockPluginManager.isPluginEnabled("Skoice")).thenReturn(true);
24+
}
25+
26+
@AfterAll
27+
static void tearDownAll() {
28+
BukkitTestSetup.mockedBukkit.close();
29+
}
30+
31+
}

src/test/java/net/clementraynaud/skoice/system/LinkedPlayerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package net.clementraynaud.skoice.system;
2121

22+
import net.clementraynaud.skoice.BukkitTestSetup;
2223
import net.clementraynaud.skoice.Skoice;
2324
import net.clementraynaud.skoice.storage.config.ConfigYamlFile;
2425
import org.bukkit.entity.Player;
@@ -38,7 +39,7 @@
3839
import static org.mockito.Mockito.when;
3940

4041
@ExtendWith(MockitoExtension.class)
41-
class LinkedPlayerTest {
42+
class LinkedPlayerTest extends BukkitTestSetup {
4243

4344
@Mock
4445
private Skoice plugin;

src/test/java/net/clementraynaud/skoice/system/NetworkTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package net.clementraynaud.skoice.system;
2121

22+
import net.clementraynaud.skoice.BukkitTestSetup;
2223
import net.clementraynaud.skoice.Skoice;
2324
import org.junit.jupiter.api.BeforeEach;
2425
import org.junit.jupiter.api.Test;
@@ -34,7 +35,7 @@
3435
import static org.mockito.Mockito.when;
3536

3637
@ExtendWith(MockitoExtension.class)
37-
class NetworkTest {
38+
class NetworkTest extends BukkitTestSetup {
3839

3940
@Mock
4041
private Skoice plugin;

src/test/java/net/clementraynaud/skoice/system/NetworksTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package net.clementraynaud.skoice.system;
2121

22+
import net.clementraynaud.skoice.BukkitTestSetup;
2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.Test;
2425
import org.junit.jupiter.api.extension.ExtendWith;
@@ -30,7 +31,7 @@
3031
import static org.mockito.Mockito.when;
3132

3233
@ExtendWith(MockitoExtension.class)
33-
class NetworksTest {
34+
class NetworksTest extends BukkitTestSetup {
3435

3536
@Mock
3637
private Network network1;

0 commit comments

Comments
 (0)