test: 更新测试依赖并优化测试代码
Some checks failed
test/timeline-server/pipeline/head There was a failure building this commit

- 添加spring-boot-starter-test和mockito-junit-jupiter测试依赖
- 简化AlbumServicePropertyTest中的photoIdLists生成器
- 统一WebSocket测试中的any()参数类型为Object.class
- 优化TestDataGenerators中的代码格式和UserProfile时间类型
This commit is contained in:
2026-02-25 16:18:24 +08:00
parent 8080750078
commit edeb4fc168
5 changed files with 152 additions and 148 deletions

View File

@@ -133,6 +133,18 @@
<artifactId>jqwik</artifactId>
<scope>test</scope>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -285,7 +285,6 @@ class AlbumServicePropertyTest {
@Provide
Arbitrary<List<String>> photoIdLists() {
return Arbitraries.integers().between(1, 10)
.flatMap(size -> Combinators.listOf(TestDataGenerators.photoIds())
.ofSize(size));
.flatMap(size -> TestDataGenerators.photoIds().list().ofSize(size));
}
}

View File

@@ -186,7 +186,7 @@ class CommentWebSocketTest {
// Simulate WebSocket failure
doThrow(new RuntimeException("WebSocket error"))
.when(messagingTemplate).convertAndSend(anyString(), any());
.when(messagingTemplate).convertAndSend(anyString(), any(Object.class));
// Act - should not throw exception
CommentDto result = commentService.createComment("user123", request);
@@ -196,6 +196,6 @@ class CommentWebSocketTest {
assertEquals("Test comment", result.getContent());
// Verify the WebSocket send was attempted
verify(messagingTemplate, times(1)).convertAndSend(anyString(), any());
verify(messagingTemplate, times(1)).convertAndSend(anyString(), any(Object.class));
}
}

View File

@@ -172,7 +172,7 @@ public class ReactionWebSocketTest {
// 模拟WebSocket发送失败
doThrow(new RuntimeException("WebSocket connection failed"))
.when(messagingTemplate).convertAndSend(anyString(), any());
.when(messagingTemplate).convertAndSend(anyString(), any(Object.class));
// Act & Assert - 不应该抛出异常
assertDoesNotThrow(() -> {
@@ -238,7 +238,7 @@ public class ReactionWebSocketTest {
reactionService.addOrUpdateReaction("user123", "STORY_ITEM", "story123", "LIKE");
// Assert - 不应该发送WebSocket消息
verify(messagingTemplate, never()).convertAndSend(anyString(), any());
verify(messagingTemplate, never()).convertAndSend(anyString(), any(Object.class));
}
@Test
@@ -250,6 +250,6 @@ public class ReactionWebSocketTest {
reactionService.removeReaction("user123", "STORY_ITEM", "story123");
// Assert - 不应该发送WebSocket消息
verify(messagingTemplate, never()).convertAndSend(anyString(), any());
verify(messagingTemplate, never()).convertAndSend(anyString(), any(Object.class));
}
}

View File

@@ -7,6 +7,7 @@ import net.jqwik.api.Arbitrary;
import net.jqwik.api.Combinators;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;
/**
@@ -25,8 +26,7 @@ public class TestDataGenerators {
albumNames(),
descriptions(),
photoIds(),
photoCounts()
).as((instanceId, userId, name, description, coverPhotoId, photoCount) -> {
photoCounts()).as((instanceId, userId, name, description, coverPhotoId, photoCount) -> {
Album album = new Album();
album.setInstanceId(instanceId);
album.setUserId(userId);
@@ -50,8 +50,7 @@ public class TestDataGenerators {
entityTypes(),
entityIds(),
userIds(),
commentContents()
).as((instanceId, entityType, entityId, userId, content) -> {
commentContents()).as((instanceId, entityType, entityId, userId, content) -> {
Comment comment = new Comment();
comment.setInstanceId(instanceId);
comment.setEntityType(entityType);
@@ -73,8 +72,7 @@ public class TestDataGenerators {
entityTypes(),
entityIds(),
userIds(),
reactionTypes()
).as((entityType, entityId, userId, reactionType) -> {
reactionTypes()).as((entityType, entityId, userId, reactionType) -> {
Reaction reaction = new Reaction();
reaction.setEntityType(entityType);
reaction.setEntityId(entityId);
@@ -95,8 +93,7 @@ public class TestDataGenerators {
themeModes(),
layoutTypes(),
cardSizes(),
displayModes()
).as((userId, themeMode, layout, cardSize, displayMode) -> {
displayModes()).as((userId, themeMode, layout, cardSize, displayMode) -> {
UserPreferences prefs = new UserPreferences();
prefs.setUserId(userId);
prefs.setThemeMode(themeMode);
@@ -119,14 +116,13 @@ public class TestDataGenerators {
return Combinators.combine(
userIds(),
urls(),
bios()
).as((userId, coverPhotoUrl, bio) -> {
bios()).as((userId, coverPhotoUrl, bio) -> {
UserProfile profile = new UserProfile();
profile.setUserId(userId);
profile.setCoverPhotoUrl(coverPhotoUrl);
profile.setBio(bio);
profile.setCreateTime(LocalDateTime.now());
profile.setUpdateTime(LocalDateTime.now());
profile.setCreateTime(new Date());
profile.setUpdateTime(new Date());
return profile;
});
}
@@ -137,8 +133,7 @@ public class TestDataGenerators {
public static Arbitrary<CreateAlbumRequest> createAlbumRequests() {
return Combinators.combine(
albumNames(),
descriptions()
).as((name, description) -> {
descriptions()).as((name, description) -> {
CreateAlbumRequest request = new CreateAlbumRequest();
request.setName(name);
request.setDescription(description);
@@ -153,8 +148,7 @@ public class TestDataGenerators {
return Combinators.combine(
entityTypes(),
entityIds(),
commentContents()
).as((entityType, entityId, content) -> {
commentContents()).as((entityType, entityId, content) -> {
CreateCommentRequest request = new CreateCommentRequest();
request.setEntityType(entityType);
request.setEntityId(entityId);
@@ -266,8 +260,7 @@ public class TestDataGenerators {
public static Arbitrary<String> monthPeriods() {
return Combinators.combine(
Arbitraries.integers().between(2020, 2024),
Arbitraries.integers().between(1, 12)
).as((year, month) -> String.format("%d-%02d", year, month));
Arbitraries.integers().between(1, 12)).as((year, month) -> String.format("%d-%02d", year, month));
}
/**