-
-
Notifications
You must be signed in to change notification settings - Fork 0
Write Tests
Steffen Wonning edited this page Jul 10, 2025
·
2 revisions
Before you begin writing tests with Cyano, it is important to understand what kind of logic you are testing and whether it actually needs a full server context or not.
For example, if you want to verify that a player has a specific ItemStack
in their inventory, you need to write a test that uses the MicrotusExtension
.
Here is a basic layout for such a test:
import net.minestom.testing.Env;
import net.minestom.testing.extension.MicrotusExtension;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(MicrotusExtension.class)
class ItemIntegrationTest {
@Test
void testPlayerHasSpecificItem(@NotNull Env env) {
// Create an instance and a player
Instance instance = env.createFlatInstance();
Player player = env.createPlayer(instance);
// Add a GRASS_BLOCK to the player's inventory
ItemStack stack = ItemStack.builder(Material.GRASS_BLOCK).build();
player.getInventory().addItemStack(stack);
// Fetch the item from the inventory
ItemStack itemStack = player.getInventory().getItemStack(0);
// Verify that the item stack is correct
assertNotNull(itemStack);
assertNotEquals(ItemStack.AIR, itemStack);
assertEquals(Material.GRASS_BLOCK, itemStack.material());
// Clean up
env.destroyInstance(instance, true); // 'true' removes each player reference before destroying the instance
}
}