faster tests by not mocking every time

This commit is contained in:
alexcrea 2024-11-14 21:55:30 +01:00
parent 366b4d3e3f
commit cafbb0ef08
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
8 changed files with 75 additions and 19 deletions

View file

@ -1,7 +1,5 @@
package io.delilaheve.util;
import org.mockbukkit.mockbukkit.entity.PlayerMock;
import org.mockbukkit.mockbukkit.inventory.ItemStackMock;
import io.delilaheve.CustomAnvil;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryType;
@ -12,6 +10,8 @@ import org.bukkit.permissions.PermissionAttachment;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockbukkit.mockbukkit.entity.PlayerMock;
import org.mockbukkit.mockbukkit.inventory.ItemStackMock;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.tests.ConfigResetCustomAnvilTest;
import xyz.alexcrea.cuanvil.util.AnvilFuseTestData;

View file

@ -1,39 +1,50 @@
package xyz.alexcrea.cuanvil.anvil;
import org.mockbukkit.mockbukkit.entity.PlayerMock;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockbukkit.mockbukkit.entity.PlayerMock;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
import xyz.alexcrea.cuanvil.tests.SharedCustomAnvilTest;
import xyz.alexcrea.cuanvil.util.AnvilFuseTestData;
import xyz.alexcrea.cuanvil.util.AnvilFuseTestUtil;
import xyz.alexcrea.cuanvil.util.CommonItemUtil;
public class AnvilFuseTests extends DefaultCustomAnvilTest {
public class AnvilFuseTests extends SharedCustomAnvilTest {
private AnvilInventory anvil;
private PlayerMock player;
private static AnvilInventory anvil;
private static PlayerMock player;
@Override
@BeforeEach
public void setUp() {
super.setUp();
@BeforeAll
public static void setUp() {
// Mock used player & open anvil
player = server.addPlayer();
Inventory anvil = server.createInventory(player, InventoryType.ANVIL);
this.anvil = (AnvilInventory) anvil;
AnvilFuseTests.anvil = (AnvilInventory) anvil;
player.openInventory(anvil);
ConfigHolder.DEFAULT_CONFIG.getConfig().set("debug_log", true);
ConfigHolder.DEFAULT_CONFIG.getConfig().set("debug_log_verbose", true);
}
@BeforeEach
public void prepareAnvil(){
anvil.clear();
}
@AfterAll
public static void tearDown() {
player = null;
anvil = null;
}
@Test
public void mergeFuseTest(){
// Literally just test a sharpness 4 + sharpness 4

View file

@ -1,6 +1,5 @@
package xyz.alexcrea.cuanvil.api;
import org.mockbukkit.mockbukkit.entity.PlayerMock;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.inventory.InventoryType;
@ -10,6 +9,7 @@ import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockbukkit.mockbukkit.entity.PlayerMock;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;

View file

@ -12,11 +12,11 @@ import org.junit.jupiter.params.provider.MethodSource;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
import xyz.alexcrea.cuanvil.tests.SharedCustomAnvilTest;
import java.util.stream.Stream;
public class DefaultConfigTests extends DefaultCustomAnvilTest {
public class DefaultConfigTests extends SharedCustomAnvilTest {
@ParameterizedTest
@MethodSource("provideStringsForIsConfiguredValueValid")

View file

@ -1,11 +1,11 @@
package xyz.alexcrea.cuanvil.mock;
import org.mockbukkit.mockbukkit.inventory.PlayerInventoryViewMock;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.view.AnvilView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mockbukkit.mockbukkit.inventory.PlayerInventoryViewMock;
@SuppressWarnings({"removal"})
public class AnvilViewMock extends PlayerInventoryViewMock implements AnvilView {

View file

@ -1,10 +1,10 @@
package xyz.alexcrea.cuanvil.mock;
import org.mockbukkit.mockbukkit.inventory.ItemStackMock;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.mockbukkit.mockbukkit.inventory.ItemStackMock;
public class EnchantedItemStackMock extends ItemStackMock {

View file

@ -1,10 +1,10 @@
package xyz.alexcrea.cuanvil.tests;
import org.mockbukkit.mockbukkit.MockBukkit;
import org.mockbukkit.mockbukkit.ServerMock;
import io.delilaheve.CustomAnvil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.mockbukkit.mockbukkit.MockBukkit;
import org.mockbukkit.mockbukkit.ServerMock;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;

View file

@ -0,0 +1,45 @@
package xyz.alexcrea.cuanvil.tests;
import io.delilaheve.CustomAnvil;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.mockbukkit.mockbukkit.MockBukkit;
import org.mockbukkit.mockbukkit.ServerMock;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
import java.util.ArrayList;
import java.util.List;
public abstract class SharedCustomAnvilTest {
protected static ServerMock server;
protected static CustomAnvil plugin;
@BeforeAll
public static void setUp() {
// Start the mock server
server = MockBukkit.mock();
// Load your plugin
plugin = MockBukkit.load(CustomAnvil.class);
// Continue initialization of the plugin
server.getScheduler().performOneTick();
}
@AfterAll
public static void tearDown() {
// Stop the mock server
MockBukkit.unmock();
// Unregister enchantments
List<CAEnchantment> toUnregister = new ArrayList<>(
CAEnchantmentRegistry.getInstance().values()
);
for (CAEnchantment caEnchantment : toUnregister) {
CAEnchantmentRegistry.getInstance().unregister(caEnchantment);
}
}
}