diff --git a/src/test/java/net/minestom/server/api/TestConnection.java b/src/test/java/net/minestom/server/api/TestConnection.java index 21519039a..db954bff1 100644 --- a/src/test/java/net/minestom/server/api/TestConnection.java +++ b/src/test/java/net/minestom/server/api/TestConnection.java @@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; public interface TestConnection { @NotNull CompletableFuture<@NotNull Player> connect(@NotNull Instance instance, @NotNull Pos pos); @@ -16,5 +17,7 @@ public interface TestConnection { interface PacketTracker { @NotNull List<@NotNull T> collect(); + +

void assertSingle(Class

packetType, Consumer

consumer); } } diff --git a/src/test/java/net/minestom/server/api/TestConnectionImpl.java b/src/test/java/net/minestom/server/api/TestConnectionImpl.java index 0554c6126..9faacf13c 100644 --- a/src/test/java/net/minestom/server/api/TestConnectionImpl.java +++ b/src/test/java/net/minestom/server/api/TestConnectionImpl.java @@ -18,6 +18,10 @@ import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; final class TestConnectionImpl implements TestConnection { private final Env env; @@ -93,5 +97,14 @@ final class TestConnectionImpl implements TestConnection { incomingTrackers.remove(this); return List.copyOf(packets); } + + @Override + public

void assertSingle(Class

packetType, Consumer

consumer) { + var packets = collect(); + assertEquals(1, packets.size(), "Expected 1 packet, got " + packets); + var packet = packets.get(0); + assertInstanceOf(packetType, packet, "Expected packet of type " + packetType.getSimpleName() + ", got " + packet.getClass().getSimpleName()); + consumer.accept((P) packet); + } } } diff --git a/src/test/java/net/minestom/server/entity/EntityTeleportIntegrationTest.java b/src/test/java/net/minestom/server/entity/EntityTeleportIntegrationTest.java index 5428b6b15..d909e396d 100644 --- a/src/test/java/net/minestom/server/entity/EntityTeleportIntegrationTest.java +++ b/src/test/java/net/minestom/server/entity/EntityTeleportIntegrationTest.java @@ -55,21 +55,13 @@ public class EntityTeleportIntegrationTest { assertEquals(teleportPosition, player.getPosition()); // Verify received packet(s) - { - var packets = tracker.collect(); - assertEquals(1, packets.size()); - var packet = ((PlayerPositionAndLookPacket) packets.get(0)); - assertEquals(teleportPosition, packet.position()); - } - + tracker.assertSingle(PlayerPositionAndLookPacket.class, + packet -> assertEquals(teleportPosition, packet.position())); // Verify broadcast packet(s) - { - var packets = viewerTracker.collect(); - assertEquals(1, packets.size()); - var packet = ((EntityTeleportPacket) packets.get(0)); + viewerTracker.assertSingle(EntityTeleportPacket.class, packet -> { assertEquals(player.getEntityId(), packet.entityId()); assertEquals(teleportPosition, packet.position()); - } + }); } @Test