generator.server.springboot.mvc.internationalized-errors.test.ApplicationExceptionTest.mustache Maven / Gradle / Ivy
package {{ packageName }}.shared.error.domain;
import static org.assertj.core.api.Assertions.*;
import java.util.Map;
import org.junit.jupiter.api.Test;
import {{ packageName }}.UnitTest;
import {{ packageName }}.shared.error.infrastructure.primary.{{ baseName }}ExceptionFactory;
@UnitTest
class {{ baseName }}ExceptionTest {
@Test
void shouldGetMinimal{{ baseName }}ExceptionFromDomain() {
{{ baseName }}Exception exception = {{ baseName }}Exception.builder(null).build();
assertThat(exception.key()).isEqualTo(StandardErrorKey.INTERNAL_SERVER_ERROR);
assertThat(exception.status()).isEqualTo(ErrorStatus.INTERNAL_SERVER_ERROR);
assertThat(exception.getMessage()).isEqualTo("An error occurred");
assertThat(exception.getCause()).isNull();
assertThat(exception.parameters()).isEmpty();
}
@Test
void shouldGetMinimal{{ baseName }}ExceptionFromPrimary() {
{{ baseName }}Exception exception = {{ baseName }}ExceptionFactory.buildEmptyException();
assertThat(exception.key()).isEqualTo(StandardErrorKey.INTERNAL_SERVER_ERROR);
assertThat(exception.status()).isEqualTo(ErrorStatus.BAD_REQUEST);
assertThat(exception.getMessage()).isEqualTo("An error occurred");
assertThat(exception.getCause()).isNull();
assertThat(exception.parameters()).isEmpty();
}
@Test
void shouldGetFull{{ baseName }}Exception() {
RuntimeException cause = new RuntimeException();
{{ baseName }}Exception exception = {{ baseName }}Exception.builder(StandardErrorKey.BAD_REQUEST)
.message("This is an error")
.cause(cause)
.addParameter("parameter", "value")
.addParameters(Map.of("key", "value"))
.status(ErrorStatus.BAD_REQUEST)
.build();
assertThat(exception.key()).isEqualTo(StandardErrorKey.BAD_REQUEST);
assertThat(exception.status()).isEqualTo(ErrorStatus.BAD_REQUEST);
assertThat(exception.getMessage()).isEqualTo("This is an error");
assertThat(exception.getCause()).isEqualTo(cause);
assertThat(exception.parameters()).containsOnly(entry("parameter", "value"), entry("key", "value"));
}
@Test
void shouldGetTechnicalErrorExceptionFromMessage() {
{{ baseName }}Exception exception = {{ baseName }}Exception.technicalError("This is a problem");
assertThat(exception.getMessage()).isEqualTo("This is a problem");
assertThat(exception.key()).isEqualTo(StandardErrorKey.INTERNAL_SERVER_ERROR);
assertThat(exception.status()).isEqualTo(ErrorStatus.INTERNAL_SERVER_ERROR);
}
@Test
void shouldGetTechnicalErrorException() {
RuntimeException cause = new RuntimeException();
{{ baseName }}Exception exception = {{ baseName }}Exception.technicalError("This is a problem", cause);
assertThat(exception.getMessage()).isEqualTo("This is a problem");
assertThat(exception.key()).isEqualTo(StandardErrorKey.INTERNAL_SERVER_ERROR);
assertThat(exception.getCause()).isEqualTo(cause);
assertThat(exception.status()).isEqualTo(ErrorStatus.INTERNAL_SERVER_ERROR);
}
@Test
void shouldGetInternalServerErrorShortcut() {
{{ baseName }}Exception exception = {{ baseName }}Exception.internalServerError(
StandardErrorKey.INTERNAL_SERVER_ERROR
).build();
assertThat(exception.status()).isEqualTo(ErrorStatus.INTERNAL_SERVER_ERROR);
}
@Test
void shouldGetBadRequestShortcut() {
{{ baseName }}Exception exception = {{ baseName }}Exception.badRequest(StandardErrorKey.BAD_REQUEST).build();
assertThat(exception.status()).isEqualTo(ErrorStatus.BAD_REQUEST);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy