Moved Bukkit tests to JUnit 5

This commit is contained in:
Rsl1122 2019-08-10 08:36:14 +03:00
parent b5fb947724
commit c3f9d77698
4 changed files with 78 additions and 63 deletions

View File

@ -25,49 +25,42 @@ import com.djrapitops.plan.system.settings.ConfigSettingKeyTest;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.WebserverSettings; import com.djrapitops.plan.system.settings.paths.WebserverSettings;
import com.djrapitops.plan.system.settings.paths.key.Setting; import com.djrapitops.plan.system.settings.paths.key.Setting;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.ClassRule; import org.junit.jupiter.api.Test;
import org.junit.Rule; import org.junit.jupiter.api.io.TempDir;
import org.junit.Test; import org.junit.platform.runner.JUnitPlatform;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import rules.BukkitComponentMocker;
import rules.ComponentMocker;
import utilities.OptionalAssert; import utilities.OptionalAssert;
import utilities.RandomData; import utilities.RandomData;
import utilities.mocks.BukkitMockComponent;
import java.nio.file.Path;
import java.util.Collection; import java.util.Collection;
import java.util.Optional; import java.util.Optional;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Test for Bukkit PlanSystem. * Test for Bukkit PlanSystem.
* *
* @author Rsl1122 * @author Rsl1122
*/ */
@RunWith(MockitoJUnitRunner.Silent.class) @RunWith(JUnitPlatform.class)
public class BukkitSystemTest { public class BukkitSystemTest {
@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
private final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500); private final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
private PlanSystem system; private PlanSystem system;
@Before @BeforeEach
public void prepareSystem() { void prepareSystem(@TempDir Path temp) throws Exception {
system = component.getPlanSystem(); system = new BukkitMockComponent(temp).getPlanSystem();
system.getConfigSystem().getConfig() system.getConfigSystem().getConfig()
.set(WebserverSettings.PORT, TEST_PORT_NUMBER); .set(WebserverSettings.PORT, TEST_PORT_NUMBER);
} }
@Test @Test
public void bukkitSystemEnables() throws EnableException { void bukkitSystemEnables() throws EnableException {
try { try {
system.enable(); system.enable();
assertTrue(system.isEnabled()); assertTrue(system.isEnabled());
@ -77,7 +70,7 @@ public class BukkitSystemTest {
} }
@Test @Test
public void correctWebAddressInDatabaseAfterEnable() throws EnableException { void correctWebAddressInDatabaseAfterEnable() throws EnableException {
try { try {
system.enable(); system.enable();
Database database = system.getDatabaseSystem().getDatabase(); Database database = system.getDatabaseSystem().getDatabase();
@ -92,7 +85,7 @@ public class BukkitSystemTest {
} }
@Test @Test
public void bukkitSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException { void bukkitSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException {
try { try {
system.enable(); system.enable();
PlanConfig config = system.getConfigSystem().getConfig(); PlanConfig config = system.getConfigSystem().getConfig();

View File

@ -23,9 +23,13 @@ import com.djrapitops.plugin.logging.console.TestPluginLogger;
import com.djrapitops.plugin.logging.error.ConsoleErrorLogger; import com.djrapitops.plugin.logging.error.ConsoleErrorLogger;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.junit.Before; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import utilities.TestConstants; import utilities.TestConstants;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -37,20 +41,33 @@ import static org.mockito.Mockito.*;
* *
* @author Rsl1122 * @author Rsl1122
*/ */
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
public class AFKListenerTest { public class AFKListenerTest {
private AFKListener underTest; private static AFKListener underTest;
@Before @BeforeAll
public void setUp() { static void setUp() {
PlanConfig config = Mockito.mock(PlanConfig.class); PlanConfig config = Mockito.mock(PlanConfig.class);
when(config.get(TimeSettings.AFK_THRESHOLD)).thenReturn(TimeUnit.MINUTES.toMillis(3)); when(config.get(TimeSettings.AFK_THRESHOLD)).thenReturn(TimeUnit.MINUTES.toMillis(3));
underTest = new AFKListener(config, new ConsoleErrorLogger(new TestPluginLogger())); underTest = new AFKListener(config, new ConsoleErrorLogger(new TestPluginLogger()));
} }
@Test @Test
public void afkPermissionIsNotCalledMoreThanOnce() { void afkPermissionIsNotCalledMoreThanOnceWhenIgnored() {
Player player = mockPlayer(); Player player = mockPlayerWithPermissions();
PlayerMoveEvent event = mockMoveEvent(player);
underTest.onMove(event);
underTest.onMove(event);
verify(player, times(1)).hasPermission(anyString());
}
@Test
void afkPermissionIsNotCalledMoreThanOnceWhenNotIgnored() {
Player player = mockPlayerWithoutPermissions();
PlayerMoveEvent event = mockMoveEvent(player); PlayerMoveEvent event = mockMoveEvent(player);
underTest.onMove(event); underTest.onMove(event);
@ -65,11 +82,18 @@ public class AFKListenerTest {
return event; return event;
} }
private Player mockPlayer() { private Player mockPlayerWithPermissions() {
Player player = Mockito.mock(Player.class); Player player = Mockito.mock(Player.class);
when(player.getUniqueId()).thenReturn(TestConstants.PLAYER_ONE_UUID); when(player.getUniqueId()).thenReturn(TestConstants.PLAYER_ONE_UUID);
when(player.hasPermission(anyString())).thenReturn(true); when(player.hasPermission(anyString())).thenReturn(true);
return player; return player;
} }
private Player mockPlayerWithoutPermissions() {
Player player = Mockito.mock(Player.class);
when(player.getUniqueId()).thenReturn(TestConstants.PLAYER_TWO_UUID);
when(player.hasPermission(anyString())).thenReturn(false);
return player;
}
} }

View File

@ -14,44 +14,47 @@
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>. * along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/ */
package rules; package utilities.mocks;
import com.djrapitops.plan.DaggerPlanBukkitComponent; import com.djrapitops.plan.DaggerPlanBukkitComponent;
import com.djrapitops.plan.Plan; import com.djrapitops.plan.Plan;
import com.djrapitops.plan.PlanBukkitComponent; import com.djrapitops.plan.PlanBukkitComponent;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
import org.junit.rules.ExternalResource;
import org.junit.rules.TemporaryFolder;
import utilities.mocks.PlanBukkitMocker;
public class BukkitComponentMocker extends ExternalResource implements ComponentMocker { import java.nio.file.Path;
private final TemporaryFolder testFolder; /**
* Test utility for creating a dagger PlanComponent using a mocked Plan.
*
* @author Rsl1122
*/
public class BukkitMockComponent {
private final Path tempDir;
private Plan planMock; private Plan planMock;
private PlanBukkitComponent component; private PlanBukkitComponent component;
public BukkitComponentMocker(TemporaryFolder testFolder) { public BukkitMockComponent(Path tempDir) {
this.testFolder = testFolder; this.tempDir = tempDir;
} }
@Override public Plan getPlanMock() throws Exception {
protected void before() throws Throwable { if (planMock == null) {
PlanBukkitMocker mocker = PlanBukkitMocker.setUp() planMock = PlanBukkitMocker.setUp()
.withDataFolder(testFolder.newFolder()) .withDataFolder(tempDir.toFile())
.withPluginDescription() .withPluginDescription()
.withResourceFetchingFromJar() .withResourceFetchingFromJar()
.withServer(); .withServer()
planMock = mocker.getPlanMock(); .getPlanMock();
component = DaggerPlanBukkitComponent.builder().plan(planMock).build();
} }
public PlanPlugin getPlanMock() {
return planMock; return planMock;
} }
public PlanSystem getPlanSystem() { public PlanSystem getPlanSystem() throws Exception {
if (component == null) {
component = DaggerPlanBukkitComponent.builder().plan(getPlanMock()).build();
}
return component.system(); return component.system();
} }
} }

View File

@ -84,17 +84,12 @@ public class PlanBukkitMocker extends Mocker {
return this; return this;
} }
public PlanBukkitMocker withDataFolder(File tempFolder) { PlanBukkitMocker withDataFolder(File tempFolder) {
doReturn(tempFolder).when(planMock).getDataFolder(); doReturn(tempFolder).when(planMock).getDataFolder();
return this; return this;
} }
@Deprecated PlanBukkitMocker withPluginDescription() {
public PlanBukkitMocker withLogging() {
return this;
}
public PlanBukkitMocker withPluginDescription() {
try { try {
File pluginYml = getFile("/plugin.yml"); File pluginYml = getFile("/plugin.yml");
PluginDescriptionFile description = new PluginDescriptionFile(new FileInputStream(pluginYml)); PluginDescriptionFile description = new PluginDescriptionFile(new FileInputStream(pluginYml));
@ -105,12 +100,12 @@ public class PlanBukkitMocker extends Mocker {
return this; return this;
} }
public PlanBukkitMocker withResourceFetchingFromJar() throws IOException { PlanBukkitMocker withResourceFetchingFromJar() throws IOException {
withPluginFiles(); withPluginFiles();
return this; return this;
} }
public PlanBukkitMocker withServer() { PlanBukkitMocker withServer() {
Server serverMock = Mockito.mock(Server.class); Server serverMock = Mockito.mock(Server.class);
doReturn("").when(serverMock).getIp(); doReturn("").when(serverMock).getIp();
doReturn("Bukkit").when(serverMock).getName(); doReturn("Bukkit").when(serverMock).getName();
@ -129,7 +124,7 @@ public class PlanBukkitMocker extends Mocker {
return this; return this;
} }
public Plan getPlanMock() { Plan getPlanMock() {
return planMock; return planMock;
} }
} }