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;
/**
@@ -20,25 +21,24 @@ public class TestDataGenerators {
*/
public static Arbitrary<Album> albums() {
return Combinators.combine(
instanceIds(),
userIds(),
albumNames(),
descriptions(),
photoIds(),
photoCounts()
).as((instanceId, userId, name, description, coverPhotoId, photoCount) -> {
Album album = new Album();
album.setInstanceId(instanceId);
album.setUserId(userId);
album.setName(name);
album.setDescription(description);
album.setCoverPhotoId(coverPhotoId);
album.setPhotoCount(photoCount);
album.setCreateTime(LocalDateTime.now());
album.setUpdateTime(LocalDateTime.now());
album.setIsDelete(0);
return album;
});
instanceIds(),
userIds(),
albumNames(),
descriptions(),
photoIds(),
photoCounts()).as((instanceId, userId, name, description, coverPhotoId, photoCount) -> {
Album album = new Album();
album.setInstanceId(instanceId);
album.setUserId(userId);
album.setName(name);
album.setDescription(description);
album.setCoverPhotoId(coverPhotoId);
album.setPhotoCount(photoCount);
album.setCreateTime(LocalDateTime.now());
album.setUpdateTime(LocalDateTime.now());
album.setIsDelete(0);
return album;
});
}
/**
@@ -46,23 +46,22 @@ public class TestDataGenerators {
*/
public static Arbitrary<Comment> comments() {
return Combinators.combine(
instanceIds(),
entityTypes(),
entityIds(),
userIds(),
commentContents()
).as((instanceId, entityType, entityId, userId, content) -> {
Comment comment = new Comment();
comment.setInstanceId(instanceId);
comment.setEntityType(entityType);
comment.setEntityId(entityId);
comment.setUserId(userId);
comment.setContent(content);
comment.setCreateTime(LocalDateTime.now());
comment.setUpdateTime(LocalDateTime.now());
comment.setIsDelete(0);
return comment;
});
instanceIds(),
entityTypes(),
entityIds(),
userIds(),
commentContents()).as((instanceId, entityType, entityId, userId, content) -> {
Comment comment = new Comment();
comment.setInstanceId(instanceId);
comment.setEntityType(entityType);
comment.setEntityId(entityId);
comment.setUserId(userId);
comment.setContent(content);
comment.setCreateTime(LocalDateTime.now());
comment.setUpdateTime(LocalDateTime.now());
comment.setIsDelete(0);
return comment;
});
}
/**
@@ -70,20 +69,19 @@ public class TestDataGenerators {
*/
public static Arbitrary<Reaction> reactions() {
return Combinators.combine(
entityTypes(),
entityIds(),
userIds(),
reactionTypes()
).as((entityType, entityId, userId, reactionType) -> {
Reaction reaction = new Reaction();
reaction.setEntityType(entityType);
reaction.setEntityId(entityId);
reaction.setUserId(userId);
reaction.setReactionType(reactionType);
reaction.setCreateTime(LocalDateTime.now());
reaction.setUpdateTime(LocalDateTime.now());
return reaction;
});
entityTypes(),
entityIds(),
userIds(),
reactionTypes()).as((entityType, entityId, userId, reactionType) -> {
Reaction reaction = new Reaction();
reaction.setEntityType(entityType);
reaction.setEntityId(entityId);
reaction.setUserId(userId);
reaction.setReactionType(reactionType);
reaction.setCreateTime(LocalDateTime.now());
reaction.setUpdateTime(LocalDateTime.now());
return reaction;
});
}
/**
@@ -91,25 +89,24 @@ public class TestDataGenerators {
*/
public static Arbitrary<UserPreferences> userPreferences() {
return Combinators.combine(
userIds(),
themeModes(),
layoutTypes(),
cardSizes(),
displayModes()
).as((userId, themeMode, layout, cardSize, displayMode) -> {
UserPreferences prefs = new UserPreferences();
prefs.setUserId(userId);
prefs.setThemeMode(themeMode);
prefs.setColorScheme("default");
prefs.setGalleryLayout(layout);
prefs.setTimelineLayout(layout);
prefs.setAlbumLayout(layout);
prefs.setCardSize(cardSize);
prefs.setTimelineDisplayMode(displayMode);
prefs.setCreateTime(LocalDateTime.now());
prefs.setUpdateTime(LocalDateTime.now());
return prefs;
});
userIds(),
themeModes(),
layoutTypes(),
cardSizes(),
displayModes()).as((userId, themeMode, layout, cardSize, displayMode) -> {
UserPreferences prefs = new UserPreferences();
prefs.setUserId(userId);
prefs.setThemeMode(themeMode);
prefs.setColorScheme("default");
prefs.setGalleryLayout(layout);
prefs.setTimelineLayout(layout);
prefs.setAlbumLayout(layout);
prefs.setCardSize(cardSize);
prefs.setTimelineDisplayMode(displayMode);
prefs.setCreateTime(LocalDateTime.now());
prefs.setUpdateTime(LocalDateTime.now());
return prefs;
});
}
/**
@@ -117,18 +114,17 @@ public class TestDataGenerators {
*/
public static Arbitrary<UserProfile> userProfiles() {
return Combinators.combine(
userIds(),
urls(),
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());
return profile;
});
userIds(),
urls(),
bios()).as((userId, coverPhotoUrl, bio) -> {
UserProfile profile = new UserProfile();
profile.setUserId(userId);
profile.setCoverPhotoUrl(coverPhotoUrl);
profile.setBio(bio);
profile.setCreateTime(new Date());
profile.setUpdateTime(new Date());
return profile;
});
}
/**
@@ -136,14 +132,13 @@ public class TestDataGenerators {
*/
public static Arbitrary<CreateAlbumRequest> createAlbumRequests() {
return Combinators.combine(
albumNames(),
descriptions()
).as((name, description) -> {
CreateAlbumRequest request = new CreateAlbumRequest();
request.setName(name);
request.setDescription(description);
return request;
});
albumNames(),
descriptions()).as((name, description) -> {
CreateAlbumRequest request = new CreateAlbumRequest();
request.setName(name);
request.setDescription(description);
return request;
});
}
/**
@@ -151,16 +146,15 @@ public class TestDataGenerators {
*/
public static Arbitrary<CreateCommentRequest> createCommentRequests() {
return Combinators.combine(
entityTypes(),
entityIds(),
commentContents()
).as((entityType, entityId, content) -> {
CreateCommentRequest request = new CreateCommentRequest();
request.setEntityType(entityType);
request.setEntityId(entityId);
request.setContent(content);
return request;
});
entityTypes(),
entityIds(),
commentContents()).as((entityType, entityId, content) -> {
CreateCommentRequest request = new CreateCommentRequest();
request.setEntityType(entityType);
request.setEntityId(entityId);
request.setContent(content);
return request;
});
}
// ========== 基础数据生成器 ==========
@@ -183,46 +177,46 @@ public class TestDataGenerators {
public static Arbitrary<String> albumNames() {
return Arbitraries.strings()
.alpha()
.ofMinLength(1)
.ofMaxLength(50)
.map(s -> "Album " + s);
.alpha()
.ofMinLength(1)
.ofMaxLength(50)
.map(s -> "Album " + s);
}
public static Arbitrary<String> descriptions() {
return Arbitraries.strings()
.alpha()
.numeric()
.withChars(' ', '.', ',')
.ofMinLength(0)
.ofMaxLength(200);
.alpha()
.numeric()
.withChars(' ', '.', ',')
.ofMinLength(0)
.ofMaxLength(200);
}
public static Arbitrary<String> commentContents() {
return Arbitraries.strings()
.alpha()
.numeric()
.withChars(' ', '.', ',', '!', '?')
.ofMinLength(1)
.ofMaxLength(1000);
.alpha()
.numeric()
.withChars(' ', '.', ',', '!', '?')
.ofMinLength(1)
.ofMaxLength(1000);
}
public static Arbitrary<String> bios() {
return Arbitraries.strings()
.alpha()
.numeric()
.withChars(' ', '.', ',')
.ofMinLength(0)
.ofMaxLength(500);
.alpha()
.numeric()
.withChars(' ', '.', ',')
.ofMinLength(0)
.ofMaxLength(500);
}
public static Arbitrary<String> urls() {
return Arbitraries.strings()
.alpha()
.numeric()
.ofMinLength(10)
.ofMaxLength(50)
.map(s -> "https://example.com/" + s + ".jpg");
.alpha()
.numeric()
.ofMinLength(10)
.ofMaxLength(50)
.map(s -> "https://example.com/" + s + ".jpg");
}
public static Arbitrary<Integer> photoCounts() {
@@ -265,9 +259,8 @@ 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(2020, 2024),
Arbitraries.integers().between(1, 12)).as((year, month) -> String.format("%d-%02d", year, month));
}
/**
@@ -275,7 +268,7 @@ public class TestDataGenerators {
*/
public static Arbitrary<String> yearPeriods() {
return Arbitraries.integers().between(2020, 2024)
.map(String::valueOf);
.map(String::valueOf);
}
/**