test(SmartCollectionServiceTest): 添加对JsonProcessingException的处理
Some checks failed
test/timeline-server/pipeline/head There was a failure building this commit

在测试中为objectMapper.writeValueAsString添加异常处理,确保测试用例能够正确处理可能出现的JsonProcessingException
This commit is contained in:
2026-02-25 16:46:34 +08:00
parent edeb4fc168
commit 1193dc70e0

View File

@@ -1,5 +1,6 @@
package com.timeline.user.service; package com.timeline.user.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.timeline.user.dao.SmartCollectionMapper; import com.timeline.user.dao.SmartCollectionMapper;
import com.timeline.user.dto.SmartCollectionDto; import com.timeline.user.dto.SmartCollectionDto;
@@ -69,8 +70,13 @@ class SmartCollectionServiceTest {
metadata.put("date", LocalDateTime.of(2024, 1, 15, 10, 30)); metadata.put("date", LocalDateTime.of(2024, 1, 15, 10, 30));
when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria( when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria(
eq(testUserId), eq("DATE"), anyString())).thenReturn(0); eq(testUserId), eq("DATE"), anyString())).thenReturn(0);
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}"); try {
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// When // When
smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata); smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata);
@@ -91,8 +97,13 @@ class SmartCollectionServiceTest {
metadata.put("location", location); metadata.put("location", location);
when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria( when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria(
eq(testUserId), eq("LOCATION"), anyString())).thenReturn(0); eq(testUserId), eq("LOCATION"), anyString())).thenReturn(0);
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}"); try {
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// When // When
smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata); smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata);
@@ -108,8 +119,13 @@ class SmartCollectionServiceTest {
metadata.put("persons", Arrays.asList("Alice", "Bob")); metadata.put("persons", Arrays.asList("Alice", "Bob"));
when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria( when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria(
eq(testUserId), eq("PERSON"), anyString())).thenReturn(0); eq(testUserId), eq("PERSON"), anyString())).thenReturn(0);
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}"); try {
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// When // When
smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata); smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata);
@@ -126,8 +142,13 @@ class SmartCollectionServiceTest {
metadata.put("date", LocalDateTime.of(2024, 1, 15, 10, 30)); metadata.put("date", LocalDateTime.of(2024, 1, 15, 10, 30));
when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria( when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria(
eq(testUserId), eq("DATE"), anyString())).thenReturn(1); eq(testUserId), eq("DATE"), anyString())).thenReturn(1);
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}"); try {
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// When // When
smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata); smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata);