Skip to content

Commit

Permalink
test(#1023): use assertj core for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lmoesle committed Nov 23, 2023
1 parent 735fd97 commit afb1e42
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@

<!-- testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
import de.muenchen.oss.digiwf.message.process.api.ErrorApi;
import de.muenchen.oss.digiwf.message.process.api.error.BpmnError;
import de.muenchen.oss.digiwf.message.process.api.error.IncidentError;
import org.junit.jupiter.api.Assertions;
import jakarta.validation.ValidationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;

import jakarta.validation.ValidationException;
import java.util.Map;

import static de.muenchen.oss.digiwf.message.common.MessageConstants.DIGIWF_MESSAGE_NAME;
import static de.muenchen.oss.digiwf.message.common.MessageConstants.DIGIWF_PROCESS_INSTANCE_ID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -74,7 +74,7 @@ void testEmailIntegrationHandlesValidationException() {
verify(monitoringServiceMock, times(1)).sendMailFailed();
final ArgumentCaptor<Map> messageHeaderArgumentCaptor = ArgumentCaptor.forClass(Map.class);
verify(errorApiMock, times(1)).handleBpmnError(messageHeaderArgumentCaptor.capture(), any(BpmnError.class));
Assertions.assertTrue(messageHeaderArgumentCaptor.getValue().containsKey(DIGIWF_PROCESS_INSTANCE_ID));
assertThat(messageHeaderArgumentCaptor.getValue()).containsKey(DIGIWF_PROCESS_INSTANCE_ID);
}

@Test
Expand All @@ -84,7 +84,7 @@ void testEmailIntegrationHandlesBpmnError() {
verify(monitoringServiceMock, times(1)).sendMailFailed();
final ArgumentCaptor<Map> messageHeaderArgumentCaptor = ArgumentCaptor.forClass(Map.class);
verify(errorApiMock, times(1)).handleBpmnError(messageHeaderArgumentCaptor.capture(), any(BpmnError.class));
Assertions.assertTrue(messageHeaderArgumentCaptor.getValue().containsKey(DIGIWF_PROCESS_INSTANCE_ID));
assertThat(messageHeaderArgumentCaptor.getValue()).containsKey(DIGIWF_PROCESS_INSTANCE_ID);
}

@Test
Expand All @@ -94,7 +94,7 @@ void testEmailIntegrationHandlesIncidentError() {
verify(monitoringServiceMock, times(1)).sendMailFailed();
final ArgumentCaptor<Map> messageHeaderArgumentCaptor = ArgumentCaptor.forClass(Map.class);
verify(errorApiMock, times(1)).handleIncident(messageHeaderArgumentCaptor.capture(), any(IncidentError.class));
Assertions.assertTrue(messageHeaderArgumentCaptor.getValue().containsKey(DIGIWF_PROCESS_INSTANCE_ID));
assertThat(messageHeaderArgumentCaptor.getValue()).containsKey(DIGIWF_PROCESS_INSTANCE_ID);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import de.muenchen.oss.digiwf.email.integration.model.PresignedUrl;
import de.muenchen.oss.digiwf.email.model.FileAttachment;
import de.muenchen.oss.digiwf.message.process.api.error.BpmnError;
import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageClientErrorException;
import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageException;
import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageServerErrorException;
import de.muenchen.oss.digiwf.s3.integration.client.repository.transfer.S3FileTransferRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
Expand All @@ -17,6 +15,7 @@
import java.util.Arrays;
import java.util.Map;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -41,7 +40,7 @@ void testLoadAttachment_DocumentStorageException() throws DocumentStorageExcepti
// DocumentStorageException
when(s3FileTransferRepository.getFile(url))
.thenThrow(new DocumentStorageException("Some error", new RuntimeException("Some error")));
Assertions.assertThrows(BpmnError.class, () -> s3Adapter.loadAttachment(presignedUrl));
assertThatThrownBy(() -> s3Adapter.loadAttachment(presignedUrl)).isInstanceOf(DocumentStorageException.class).hasMessage("Some error");
}

@Test
Expand All @@ -62,12 +61,12 @@ void testLoadAttachment_Success() throws DocumentStorageException, DocumentStora
new PresignedUrl("http://localhost:9000/" + file, path, "GET")
);

Assertions.assertTrue(Arrays.equals(testFile, fileAttachment.getFile().getInputStream().readAllBytes()));
Assertions.assertEquals(file.getKey(), fileAttachment.getFileName());
Assertions.assertEquals(file.getValue(), fileAttachment.getFile().getContentType());
assertThat(Arrays.equals(testFile, fileAttachment.getFile().getInputStream().readAllBytes())).isTrue();
assertThat(file.getKey()).isEqualTo(fileAttachment.getFileName());
assertThat(file.getValue()).isEqualTo(fileAttachment.getFile().getContentType());
} catch (final IOException e) {
log.warn("Could not read file: {}", file);
Assertions.fail(e);
fail(e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.*;

class SendMailUseCaseTest {
Expand Down Expand Up @@ -85,6 +85,6 @@ void sendMailWithAttachments() throws MessagingException {
@Test
void sendMailThrowsBpmnError() throws MessagingException {
doThrow(new MessagingException("Test Exception")).when(mailPort).sendMail(any());
assertThrows(BpmnError.class, () -> sendMail.sendMail(processInstanceId, messageName, mail));
assertThatThrownBy(() -> sendMail.sendMail(processInstanceId, messageName, mail)).isInstanceOf(BpmnError.class);
}
}

0 comments on commit afb1e42

Please sign in to comment.