test: 更新测试依赖并优化测试代码
Some checks failed
test/timeline-server/pipeline/head There was a failure building this commit
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:
@@ -133,6 +133,18 @@
|
|||||||
<artifactId>jqwik</artifactId>
|
<artifactId>jqwik</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -285,7 +285,6 @@ class AlbumServicePropertyTest {
|
|||||||
@Provide
|
@Provide
|
||||||
Arbitrary<List<String>> photoIdLists() {
|
Arbitrary<List<String>> photoIdLists() {
|
||||||
return Arbitraries.integers().between(1, 10)
|
return Arbitraries.integers().between(1, 10)
|
||||||
.flatMap(size -> Combinators.listOf(TestDataGenerators.photoIds())
|
.flatMap(size -> TestDataGenerators.photoIds().list().ofSize(size));
|
||||||
.ofSize(size));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ class CommentWebSocketTest {
|
|||||||
|
|
||||||
// Simulate WebSocket failure
|
// Simulate WebSocket failure
|
||||||
doThrow(new RuntimeException("WebSocket error"))
|
doThrow(new RuntimeException("WebSocket error"))
|
||||||
.when(messagingTemplate).convertAndSend(anyString(), any());
|
.when(messagingTemplate).convertAndSend(anyString(), any(Object.class));
|
||||||
|
|
||||||
// Act - should not throw exception
|
// Act - should not throw exception
|
||||||
CommentDto result = commentService.createComment("user123", request);
|
CommentDto result = commentService.createComment("user123", request);
|
||||||
@@ -196,6 +196,6 @@ class CommentWebSocketTest {
|
|||||||
assertEquals("Test comment", result.getContent());
|
assertEquals("Test comment", result.getContent());
|
||||||
|
|
||||||
// Verify the WebSocket send was attempted
|
// Verify the WebSocket send was attempted
|
||||||
verify(messagingTemplate, times(1)).convertAndSend(anyString(), any());
|
verify(messagingTemplate, times(1)).convertAndSend(anyString(), any(Object.class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ public class ReactionWebSocketTest {
|
|||||||
|
|
||||||
// 模拟WebSocket发送失败
|
// 模拟WebSocket发送失败
|
||||||
doThrow(new RuntimeException("WebSocket connection failed"))
|
doThrow(new RuntimeException("WebSocket connection failed"))
|
||||||
.when(messagingTemplate).convertAndSend(anyString(), any());
|
.when(messagingTemplate).convertAndSend(anyString(), any(Object.class));
|
||||||
|
|
||||||
// Act & Assert - 不应该抛出异常
|
// Act & Assert - 不应该抛出异常
|
||||||
assertDoesNotThrow(() -> {
|
assertDoesNotThrow(() -> {
|
||||||
@@ -238,7 +238,7 @@ public class ReactionWebSocketTest {
|
|||||||
reactionService.addOrUpdateReaction("user123", "STORY_ITEM", "story123", "LIKE");
|
reactionService.addOrUpdateReaction("user123", "STORY_ITEM", "story123", "LIKE");
|
||||||
|
|
||||||
// Assert - 不应该发送WebSocket消息
|
// Assert - 不应该发送WebSocket消息
|
||||||
verify(messagingTemplate, never()).convertAndSend(anyString(), any());
|
verify(messagingTemplate, never()).convertAndSend(anyString(), any(Object.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -250,6 +250,6 @@ public class ReactionWebSocketTest {
|
|||||||
reactionService.removeReaction("user123", "STORY_ITEM", "story123");
|
reactionService.removeReaction("user123", "STORY_ITEM", "story123");
|
||||||
|
|
||||||
// Assert - 不应该发送WebSocket消息
|
// Assert - 不应该发送WebSocket消息
|
||||||
verify(messagingTemplate, never()).convertAndSend(anyString(), any());
|
verify(messagingTemplate, never()).convertAndSend(anyString(), any(Object.class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import net.jqwik.api.Arbitrary;
|
|||||||
import net.jqwik.api.Combinators;
|
import net.jqwik.api.Combinators;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,8 +26,7 @@ public class TestDataGenerators {
|
|||||||
albumNames(),
|
albumNames(),
|
||||||
descriptions(),
|
descriptions(),
|
||||||
photoIds(),
|
photoIds(),
|
||||||
photoCounts()
|
photoCounts()).as((instanceId, userId, name, description, coverPhotoId, photoCount) -> {
|
||||||
).as((instanceId, userId, name, description, coverPhotoId, photoCount) -> {
|
|
||||||
Album album = new Album();
|
Album album = new Album();
|
||||||
album.setInstanceId(instanceId);
|
album.setInstanceId(instanceId);
|
||||||
album.setUserId(userId);
|
album.setUserId(userId);
|
||||||
@@ -50,8 +50,7 @@ public class TestDataGenerators {
|
|||||||
entityTypes(),
|
entityTypes(),
|
||||||
entityIds(),
|
entityIds(),
|
||||||
userIds(),
|
userIds(),
|
||||||
commentContents()
|
commentContents()).as((instanceId, entityType, entityId, userId, content) -> {
|
||||||
).as((instanceId, entityType, entityId, userId, content) -> {
|
|
||||||
Comment comment = new Comment();
|
Comment comment = new Comment();
|
||||||
comment.setInstanceId(instanceId);
|
comment.setInstanceId(instanceId);
|
||||||
comment.setEntityType(entityType);
|
comment.setEntityType(entityType);
|
||||||
@@ -73,8 +72,7 @@ public class TestDataGenerators {
|
|||||||
entityTypes(),
|
entityTypes(),
|
||||||
entityIds(),
|
entityIds(),
|
||||||
userIds(),
|
userIds(),
|
||||||
reactionTypes()
|
reactionTypes()).as((entityType, entityId, userId, reactionType) -> {
|
||||||
).as((entityType, entityId, userId, reactionType) -> {
|
|
||||||
Reaction reaction = new Reaction();
|
Reaction reaction = new Reaction();
|
||||||
reaction.setEntityType(entityType);
|
reaction.setEntityType(entityType);
|
||||||
reaction.setEntityId(entityId);
|
reaction.setEntityId(entityId);
|
||||||
@@ -95,8 +93,7 @@ public class TestDataGenerators {
|
|||||||
themeModes(),
|
themeModes(),
|
||||||
layoutTypes(),
|
layoutTypes(),
|
||||||
cardSizes(),
|
cardSizes(),
|
||||||
displayModes()
|
displayModes()).as((userId, themeMode, layout, cardSize, displayMode) -> {
|
||||||
).as((userId, themeMode, layout, cardSize, displayMode) -> {
|
|
||||||
UserPreferences prefs = new UserPreferences();
|
UserPreferences prefs = new UserPreferences();
|
||||||
prefs.setUserId(userId);
|
prefs.setUserId(userId);
|
||||||
prefs.setThemeMode(themeMode);
|
prefs.setThemeMode(themeMode);
|
||||||
@@ -119,14 +116,13 @@ public class TestDataGenerators {
|
|||||||
return Combinators.combine(
|
return Combinators.combine(
|
||||||
userIds(),
|
userIds(),
|
||||||
urls(),
|
urls(),
|
||||||
bios()
|
bios()).as((userId, coverPhotoUrl, bio) -> {
|
||||||
).as((userId, coverPhotoUrl, bio) -> {
|
|
||||||
UserProfile profile = new UserProfile();
|
UserProfile profile = new UserProfile();
|
||||||
profile.setUserId(userId);
|
profile.setUserId(userId);
|
||||||
profile.setCoverPhotoUrl(coverPhotoUrl);
|
profile.setCoverPhotoUrl(coverPhotoUrl);
|
||||||
profile.setBio(bio);
|
profile.setBio(bio);
|
||||||
profile.setCreateTime(LocalDateTime.now());
|
profile.setCreateTime(new Date());
|
||||||
profile.setUpdateTime(LocalDateTime.now());
|
profile.setUpdateTime(new Date());
|
||||||
return profile;
|
return profile;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -137,8 +133,7 @@ public class TestDataGenerators {
|
|||||||
public static Arbitrary<CreateAlbumRequest> createAlbumRequests() {
|
public static Arbitrary<CreateAlbumRequest> createAlbumRequests() {
|
||||||
return Combinators.combine(
|
return Combinators.combine(
|
||||||
albumNames(),
|
albumNames(),
|
||||||
descriptions()
|
descriptions()).as((name, description) -> {
|
||||||
).as((name, description) -> {
|
|
||||||
CreateAlbumRequest request = new CreateAlbumRequest();
|
CreateAlbumRequest request = new CreateAlbumRequest();
|
||||||
request.setName(name);
|
request.setName(name);
|
||||||
request.setDescription(description);
|
request.setDescription(description);
|
||||||
@@ -153,8 +148,7 @@ public class TestDataGenerators {
|
|||||||
return Combinators.combine(
|
return Combinators.combine(
|
||||||
entityTypes(),
|
entityTypes(),
|
||||||
entityIds(),
|
entityIds(),
|
||||||
commentContents()
|
commentContents()).as((entityType, entityId, content) -> {
|
||||||
).as((entityType, entityId, content) -> {
|
|
||||||
CreateCommentRequest request = new CreateCommentRequest();
|
CreateCommentRequest request = new CreateCommentRequest();
|
||||||
request.setEntityType(entityType);
|
request.setEntityType(entityType);
|
||||||
request.setEntityId(entityId);
|
request.setEntityId(entityId);
|
||||||
@@ -266,8 +260,7 @@ public class TestDataGenerators {
|
|||||||
public static Arbitrary<String> monthPeriods() {
|
public static Arbitrary<String> monthPeriods() {
|
||||||
return Combinators.combine(
|
return Combinators.combine(
|
||||||
Arbitraries.integers().between(2020, 2024),
|
Arbitraries.integers().between(2020, 2024),
|
||||||
Arbitraries.integers().between(1, 12)
|
Arbitraries.integers().between(1, 12)).as((year, month) -> String.format("%d-%02d", year, month));
|
||||||
).as((year, month) -> String.format("%d-%02d", year, month));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user