From 1e281ff8ca9db92269a731bc81913f6a93261ffa Mon Sep 17 00:00:00 2001 From: Azure <983547216@qq.com> Date: Mon, 22 Jun 2026 13:50:02 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20SSE=20=E9=9B=86?= =?UTF-8?q?=E6=88=90=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 验证 SseEventPublisher 的 Bean 注入、broadcast 和 sendTo 方法。 共 3 个测试用例全部通过。 --- .../sse/SseIntegrationTest.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/test/java/com/webgame/webgamebackend/sse/SseIntegrationTest.java diff --git a/src/test/java/com/webgame/webgamebackend/sse/SseIntegrationTest.java b/src/test/java/com/webgame/webgamebackend/sse/SseIntegrationTest.java new file mode 100644 index 0000000..3c1dd0f --- /dev/null +++ b/src/test/java/com/webgame/webgamebackend/sse/SseIntegrationTest.java @@ -0,0 +1,49 @@ +package com.webgame.webgamebackend.sse; + +import com.alibaba.fastjson2.JSON; +import com.webgame.webgamebackend.common.event.SseEventPublisher; +import com.webgame.webgamebackend.common.event.SseEventType; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * SSE 集成测试 + * + * 验证 SSE 事件发布器的基本功能。 + * 需要 Redis 和 MySQL 运行。 + */ +@SpringBootTest +@DisplayName("SSE 集成测试") +class SseIntegrationTest { + + @Autowired + private SseEventPublisher publisher; + + @Test + @DisplayName("SseEventPublisher Bean 应被正确注入") + void publisher_shouldBeInjected() { + assertThat(publisher).isNotNull(); + } + + @Test + @DisplayName("broadcast 方法不应抛异常") + void broadcast_shouldNotThrowException() { + // Act & Assert: 发布广播事件不应抛异常 + publisher.broadcast(SseEventType.ROOM_CREATED, + Map.of("roomId", "test-001", "name", "测试房间")); + } + + @Test + @DisplayName("sendTo 方法不应抛异常") + void sendTo_shouldNotThrowException() { + // Act & Assert: 发布定向事件不应抛异常 + publisher.sendTo("test-user-id", SseEventType.KICKED, + Map.of("roomId", "test-001")); + } +}