66import static org .mockito .ArgumentMatchers .any ;
77import static org .mockito .ArgumentMatchers .anyString ;
88import static org .mockito .ArgumentMatchers .eq ;
9+ import static org .mockito .Mockito .mockStatic ;
910import static org .mockito .Mockito .never ;
1011import static org .mockito .Mockito .times ;
1112import static org .mockito .Mockito .verify ;
1516import java .io .FileInputStream ;
1617import java .io .FileOutputStream ;
1718import java .io .IOException ;
19+ import java .lang .reflect .Field ;
1820import java .nio .file .Files ;
1921import java .nio .file .Path ;
2022import java .nio .file .Paths ;
3436import org .junit .Test ;
3537import org .junit .runner .RunWith ;
3638import org .mockito .Mock ;
37- import org .powermock .api .mockito .PowerMockito ;
38- import org .powermock .core .classloader .annotations .PrepareForTest ;
39- import org .powermock .modules .junit4 .PowerMockRunner ;
40- import org .powermock .reflect .Whitebox ;
39+ import org .mockito .MockedStatic ;
40+ import org .mockito .Mockito ;
41+ import org .mockito .junit .MockitoJUnitRunner ;
4142
4243import com .wasteofplastic .invswitcher .listeners .PlayerListener ;
4344
5253 * @author tastybento
5354 *
5455 */
55- @ RunWith (PowerMockRunner .class )
56- @ PrepareForTest ({Bukkit .class , BentoBox .class })
56+ @ RunWith (MockitoJUnitRunner .class )
5757public class InvSwitcherTest {
5858
5959 private static File jFile ;
@@ -95,11 +95,20 @@ public static void beforeClass() throws IOException {
9595 }
9696
9797 /**
98+ * @throws SecurityException
99+ * @throws NoSuchFieldException
100+ * @throws IllegalAccessException
101+ * @throws IllegalArgumentException
98102 */
99103 @ Before
100- public void setUp () {
104+ public void setUp ()
105+ throws NoSuchFieldException , SecurityException , IllegalArgumentException , IllegalAccessException {
101106 // Set up plugin
102- Whitebox .setInternalState (BentoBox .class , "instance" , plugin );
107+ // Use reflection to set the private static field "instance" in BentoBox
108+ Field instanceField = BentoBox .class .getDeclaredField ("instance" );
109+
110+ instanceField .setAccessible (true );
111+ instanceField .set (null , plugin );
103112 when (plugin .getLogger ()).thenReturn (Logger .getAnonymousLogger ());
104113
105114 // The database type has to be created one line before the thenReturn() to work!
@@ -117,10 +126,6 @@ public void setUp() {
117126 // Addons manager
118127 when (plugin .getAddonsManager ()).thenReturn (am );
119128
120- // Bukkit
121- PowerMockito .mockStatic (Bukkit .class );
122- when (Bukkit .getWorld (anyString ())).thenReturn (world );
123-
124129 // World
125130 when (world .getName ()).thenReturn ("bskyblock-world" );
126131 }
@@ -165,12 +170,17 @@ public void testOnEnable() {
165170 */
166171 @ Test
167172 public void testOnDisable () {
168- addon .onLoad ();
169- addon .getSettings ().setWorlds (Set .of ("bskyblock-world" ));
170- addon .allLoaded ();
171- addon .onDisable ();
172- PowerMockito .verifyStatic (Bukkit .class );
173- Bukkit .getOnlinePlayers ();
173+ // Mock the static method
174+ try (MockedStatic <Bukkit > mockedBukkit = mockStatic (Bukkit .class , Mockito .RETURNS_MOCKS )) {
175+ when (Bukkit .getWorld (anyString ())).thenReturn (world );
176+ // Run code to test
177+ addon .onLoad ();
178+ addon .getSettings ().setWorlds (Set .of ("bskyblock-world" ));
179+ addon .allLoaded ();
180+ addon .onDisable ();
181+ // Verify that the static method was never called
182+ mockedBukkit .verify (() -> Bukkit .getOnlinePlayers ());
183+ }
174184 }
175185
176186 /**
@@ -188,12 +198,17 @@ public void testOnLoad() {
188198 */
189199 @ Test
190200 public void testAllLoaded () {
191- addon .onLoad ();
192- addon .getSettings ().setWorlds (Set .of ("bskyblock-world" ));
193- addon .allLoaded ();
194- verify (plugin ).log ("[InvSwitcher] Hooking into the following worlds:" );
195- verify (plugin , times (3 )).log ("[InvSwitcher] bskyblock-world" );
196- verify (am ).registerListener (eq (addon ), any (PlayerListener .class ));
201+ // Mock the static method
202+ try (MockedStatic <Bukkit > mockedBukkit = mockStatic (Bukkit .class , Mockito .RETURNS_MOCKS )) {
203+ when (Bukkit .getWorld (anyString ())).thenReturn (world );
204+ // Run code to test
205+ addon .onLoad ();
206+ addon .getSettings ().setWorlds (Set .of ("bskyblock-world" ));
207+ addon .allLoaded ();
208+ verify (plugin ).log ("[InvSwitcher] Hooking into the following worlds:" );
209+ verify (plugin , times (3 )).log ("[InvSwitcher] bskyblock-world" );
210+ verify (am ).registerListener (eq (addon ), any (PlayerListener .class ));
211+ }
197212
198213 }
199214
0 commit comments