你好,我正在使用Couchbase Java SDK 3.2.6和Spring2.6.6和Junit5,当一些异常在测试中抛出时,它们都失败了,因为在异常消息的末尾添加了“NULL”关键字。
测试
import com.couchbase.client.core.error.DocumentNotFoundException;
import com.couchbase.client.core.error.context.ErrorContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mapstruct.factory.Mappers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class TaskServiceTest {
@InjectMocks
private TaskService taskService;
@Mock
private TaskRepository taskRepository;
@Test
void shouldThrowDocumentNotFoundExceptionWithGettingTaskByIdForFindTaskById() {
//Given
var exceptionMessage = "Document with the given id not found";
var wrongTaskId = "12345678";
when(taskRepository.findTaskById(wrongTaskId)).thenThrow(new DocumentNotFoundException(mock(ErrorContext.class)));
//When
var exception = assertThrows(DocumentNotFoundException.class, () -> taskService.getTaskDetail(wrongTaskId));
//Then
assertEquals(exceptionMessage, exception.getLocalizedMessage());
}
}
错误消息
org.opentest4j.AssertionFailedError:
Expected :Document with the given id not found
Actual :Document with the given id not found null
编辑:
在CouchbaseExceptions
类中,此getMessage
方法返回正确的错误消息+NULL
public CouchbaseException(String message, Throwable cause, ErrorContext ctx) {
super(message, cause);
this.ctx = ctx;
}
public final String getMessage() {
String output = super.getMessage();
return this.ctx != null ? output + " " + this.ctx.exportAsString(ExportFormat.JSON) : output;
}
1条答案
按热度按时间efzxgjgh1#
这些例外已经被添加了一个背景。您的模拟ErrorContext没有实现exportAsString(),因此它的计算结果为空。