Spring 2.6.6 将 Null 关键字添加到 couchbase 异常中
您好,我正在使用 Couchbase Java SDK 3.2.6 和 Spring 2.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());
}
}
errorMessage
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;
}
Hi I am using Couchbase Java SDK 3.2.6 with Spring 2.6.6 and junit5
When some exception thrown in tests, all of them are failing becuase "null" keyword added at the end of exception message.
Test
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());
}
}
errorMessage
org.opentest4j.AssertionFailedError:
Expected :Document with the given id not found
Actual :Document with the given id not found null
Edit:
In the CouchbaseExceptions
class this getMessage
method returns proper error message + 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例外情况添加了上下文。您的模拟错误context不会实现ExportAsstring(),因此它将评估为null。
The exceptions have had a context added to them. Your mock ErrorContext doesn't implement exportAsString() therefore it evaluates to null.