Skip to content

Commit

Permalink
Rewrote PanelItemTest to avoid null check errors in Bukkit class
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Dec 9, 2023
1 parent 1ab2ff2 commit 5a193cd
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/test/java/world/bentobox/bentobox/api/panels/PanelItemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
Expand All @@ -30,29 +33,35 @@
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class })
public class PanelItemTest {

@Mock
private PanelItemBuilder pib;
private PanelItem pi;
@Mock
private ClickHandler clickHandler;
@Mock
private Server server;
@Mock
private ItemFactory itemFac;

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
// Builder
when(pib.getAmount()).thenReturn(2);
when(pib.getClickHandler()).thenReturn(clickHandler);
when(pib.getDescription()).thenReturn(List.of("Description", "hello"));
when(pib.getIcon()).thenReturn(new ItemStack(Material.STONE));
when(pib.getName()).thenReturn("Name");
when(pib.getPlayerHeadName()).thenReturn("tastybento");
pi = new PanelItem(pib);
World world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
when(server.getItemFactory()).thenReturn(itemFac);

if (Bukkit.getServer() == null) {
Bukkit.setServer(server);
}

pib = new PanelItemBuilder().amount(2).clickHandler(clickHandler).description(List.of("Description", "hello"))
.icon(new ItemStack(Material.STONE)).name("Name");
pi = pib.build();
}

/**
Expand Down Expand Up @@ -96,7 +105,7 @@ public void testGetDescription() {
@Test
public void testSetDescription() {
assertEquals(2, pi.getDescription().size());
pi.setDescription(List.of("1","2","3"));
pi.setDescription(List.of("1", "2", "3"));
assertEquals(3, pi.getDescription().size());
}

Expand Down Expand Up @@ -178,6 +187,10 @@ public void testSetGlow() {
*/
@Test
public void testIsPlayerHead() {
// Make a head
pib = new PanelItemBuilder().amount(2).clickHandler(clickHandler).description(List.of("Description", "hello"))
.icon("tastybento").name("Name");
pi = pib.build();
assertTrue(pi.isPlayerHead());

}
Expand All @@ -187,6 +200,11 @@ public void testIsPlayerHead() {
*/
@Test
public void testGetPlayerHeadName() {
// Make a head
pib = new PanelItemBuilder().amount(2).clickHandler(clickHandler).description(List.of("Description", "hello"))
.icon("tastybento").name("Name");
pi = pib.build();

assertEquals("tastybento", pi.getPlayerHeadName());
}

Expand Down

0 comments on commit 5a193cd

Please sign in to comment.