test: 添加 SSE 集成测试

验证 SseEventPublisher 的 Bean 注入、broadcast 和 sendTo 方法。
共 3 个测试用例全部通过。
This commit is contained in:
2026-06-22 13:50:02 +08:00
parent bf09b7f342
commit 1e281ff8ca

View File

@@ -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"));
}
}