Spring 2.6.6 将 Null 关键字添加到 couchbase 异常中

发布于 2025-01-20 11:43:11 字数 2045 浏览 1 评论 0原文

您好,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长发绾君心 2025-01-27 11:43:11

例外情况添加了上下文。您的模拟错误context不会实现ExportAsstring(),因此它将评估为null。

The exceptions have had a context added to them. Your mock ErrorContext doesn't implement exportAsString() therefore it evaluates to null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文