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;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.timeline.user.dao.SmartCollectionMapper;
import com.timeline.user.dto.SmartCollectionDto;
@@ -67,10 +68,15 @@ class SmartCollectionServiceTest {
// Given
Map<String, Object> metadata = new HashMap<>();
metadata.put("date", LocalDateTime.of(2024, 1, 15, 10, 30));
when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria(
eq(testUserId), eq("DATE"), anyString())).thenReturn(0);
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
eq(testUserId), eq("DATE"), anyString())).thenReturn(0);
try {
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// When
smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata);
@@ -89,10 +95,15 @@ class SmartCollectionServiceTest {
location.put("latitude", 48.8566);
location.put("longitude", 2.3522);
metadata.put("location", location);
when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria(
eq(testUserId), eq("LOCATION"), anyString())).thenReturn(0);
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
eq(testUserId), eq("LOCATION"), anyString())).thenReturn(0);
try {
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// When
smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata);
@@ -106,10 +117,15 @@ class SmartCollectionServiceTest {
// Given
Map<String, Object> metadata = new HashMap<>();
metadata.put("persons", Arrays.asList("Alice", "Bob"));
when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria(
eq(testUserId), eq("PERSON"), anyString())).thenReturn(0);
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
eq(testUserId), eq("PERSON"), anyString())).thenReturn(0);
try {
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// When
smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata);
@@ -124,10 +140,15 @@ class SmartCollectionServiceTest {
// Given
Map<String, Object> metadata = new HashMap<>();
metadata.put("date", LocalDateTime.of(2024, 1, 15, 10, 30));
when(smartCollectionMapper.existsByUserIdAndTypeAndCriteria(
eq(testUserId), eq("DATE"), anyString())).thenReturn(1);
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
eq(testUserId), eq("DATE"), anyString())).thenReturn(1);
try {
when(objectMapper.writeValueAsString(anyMap())).thenReturn("{}");
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// When
smartCollectionService.processPhotoMetadata(testUserId, "photo123", metadata);