Skip to content

Commit e459a31

Browse files
tastybentoclaude
andcommitted
Prime Bukkit material registry in JUnit 5 panel tests
The CI build for PR #397 erupted with 286 errors: `NoSuchElementException: No value for minecraft:acacia_hanging_sign` thrown from `org.bukkit.inventory.ItemType.<clinit>` inside `ItemStackMock.<clinit>`, which then poisoned `ItemStackMock` for the rest of the JVM. Root cause: ItemType's static initializer iterates Material and looks each entry up in the registry. If it runs before MockBukkit has populated the registry, the static init errors and every subsequent test that touches ItemStack fails with NoClassDefFoundError. AbstractChallengesTest already worked around this by touching `Tag.LEAVES` right after `MockBukkit.mock()`, but seven JUnit 5 panel tests didn't extend it and didn't carry the workaround. The flake didn't surface locally because filesystem ordering on macOS put a primed test first; ubuntu-latest's ordering didn't. Extract `PanelTestHelper.primeBukkitRegistry()` and call it right after `MockBukkit.mock()` in CommonPanelTest, CommonPagedPanelTest, ConversationUtilsTest, AdminPanelTest, ListChallengesPanelTest, GameModePanelTest, and MultiplePanelTest. The fix is order-independent — each affected test class now primes the registry itself. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 6d8dfbd commit e459a31

8 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/test/java/world/bentobox/challenges/panel/CommonPagedPanelTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public int getPageIndex() throws Exception {
116116
void setUp() {
117117
closeable = MockitoAnnotations.openMocks(this);
118118
ServerMock mbServer = MockBukkit.mock();
119+
PanelTestHelper.primeBukkitRegistry();
119120

120121
when(addon.getChallengesManager()).thenReturn(manager);
121122
PanelTestHelper.setupUserTranslations(user);

src/test/java/world/bentobox/challenges/panel/CommonPanelTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public PanelItem getReturnButton() {
7878
void setUp() {
7979
closeable = MockitoAnnotations.openMocks(this);
8080
ServerMock mbServer = MockBukkit.mock();
81+
PanelTestHelper.primeBukkitRegistry();
8182

8283
when(addon.getChallengesManager()).thenReturn(manager);
8384
PanelTestHelper.setupUserTranslations(user);

src/test/java/world/bentobox/challenges/panel/ConversationUtilsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ConversationUtilsTest {
4545
void setUp() {
4646
closeable = MockitoAnnotations.openMocks(this);
4747
ServerMock mbServer = MockBukkit.mock();
48+
PanelTestHelper.primeBukkitRegistry();
4849

4950
when(user.getTranslation(anyString())).thenAnswer(
5051
(Answer<String>) inv -> inv.getArgument(0, String.class));

src/test/java/world/bentobox/challenges/panel/PanelTestHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@
2222
*/
2323
public class PanelTestHelper {
2424

25+
/**
26+
* Force Bukkit's material registry to be populated while MockBukkit's mock
27+
* server is active. Call this immediately after `MockBukkit.mock()`.
28+
*
29+
* <p>Why: `org.bukkit.inventory.ItemType.&lt;clinit&gt;` iterates over Material and
30+
* looks each entry up in the registry. If it runs before MockBukkit has populated
31+
* the registry (e.g., when a panel test that doesn't extend AbstractChallengesTest
32+
* runs first on CI's filesystem ordering), it throws NoSuchElementException, and
33+
* ItemStackMock stays in an errored state for the rest of the JVM — poisoning every
34+
* subsequent test. Touching `Tag.LEAVES` forces the registry to populate before
35+
* ItemType loads.
36+
*/
37+
public static void primeBukkitRegistry() {
38+
@SuppressWarnings("unused")
39+
var unused = org.bukkit.Tag.LEAVES;
40+
}
41+
2542
/**
2643
* Set up user translation mocks to return the key for any invocation.
2744
* Uses a lenient default answer that returns the first String argument

src/test/java/world/bentobox/challenges/panel/admin/AdminPanelTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class AdminPanelTest {
5454
void setUp() throws Exception {
5555
closeable = MockitoAnnotations.openMocks(this);
5656
ServerMock mbServer = MockBukkit.mock();
57+
PanelTestHelper.primeBukkitRegistry();
5758

5859
when(addon.getChallengesManager()).thenReturn(manager);
5960
PanelTestHelper.setupUserTranslations(user);

src/test/java/world/bentobox/challenges/panel/admin/ListChallengesPanelTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ListChallengesPanelTest {
4747
void setUp() {
4848
closeable = MockitoAnnotations.openMocks(this);
4949
ServerMock mbServer = MockBukkit.mock();
50+
PanelTestHelper.primeBukkitRegistry();
5051

5152
when(addon.getChallengesManager()).thenReturn(manager);
5253
PanelTestHelper.setupUserTranslations(user);

src/test/java/world/bentobox/challenges/panel/user/GameModePanelTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class GameModePanelTest {
5858
void setUp() {
5959
closeable = MockitoAnnotations.openMocks(this);
6060
ServerMock mbServer = MockBukkit.mock();
61+
PanelTestHelper.primeBukkitRegistry();
6162

6263
when(addon.getChallengesManager()).thenReturn(manager);
6364
PanelTestHelper.setupUserTranslations(user);

src/test/java/world/bentobox/challenges/panel/user/MultiplePanelTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class MultiplePanelTest {
5353
void setUp() {
5454
closeable = MockitoAnnotations.openMocks(this);
5555
ServerMock mbServer = MockBukkit.mock();
56+
PanelTestHelper.primeBukkitRegistry();
5657

5758
PanelTestHelper.setupUserTranslations(user);
5859
when(user.getWorld()).thenReturn(world);

0 commit comments

Comments
 (0)