Null keyword added to couchbase exceptions with spring 2.6.6

252 views Asked by At

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;
    }
1

There are 1 answers

0
Michael Reiche On

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