Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AssertJ for exceptions #2494

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
sanderploegsma marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.*;

import org.junit.Ignore;
import org.junit.Test;
Expand All @@ -15,116 +11,94 @@ public class ErrorHandlingTest {

@Test
public void testThrowIllegalArgumentException() {
assertThrows(
IllegalArgumentException.class,
errorHandling::handleErrorByThrowingIllegalArgumentException);
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingIllegalArgumentException());
}

@Ignore("Remove to run test")
@Test
public void testThrowIllegalArgumentExceptionWithDetailMessage() {
IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> errorHandling
.handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage(
"This is the detail message."));

assertThat(expected).hasMessage("This is the detail message.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage(
"This is the detail message."))
.withMessage("This is the detail message.");
}

@Ignore("Remove to run test")
@Test
public void testThrowAnyCheckedException() {
Exception expected =
assertThrows(
Exception.class,
errorHandling::handleErrorByThrowingAnyCheckedException);
assertThat(expected).isNotInstanceOf(RuntimeException.class);
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingAnyCheckedException())
.isNotInstanceOf(RuntimeException.class);
}

@Ignore("Remove to run test")
@Test
public void testThrowAnyCheckedExceptionWithDetailMessage() {
Exception expected =
assertThrows(
Exception.class,
() -> errorHandling
.handleErrorByThrowingAnyCheckedExceptionWithDetailMessage(
"This is the detail message."));
assertThat(expected).isNotInstanceOf(RuntimeException.class);
assertThat(expected).hasMessage("This is the detail message.");
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingAnyCheckedExceptionWithDetailMessage(
"This is the detail message."))
.isNotInstanceOf(RuntimeException.class)
.withMessage("This is the detail message.");
}

@Ignore("Remove to run test")
@Test
public void testThrowAnyUncheckedException() {
assertThrows(
RuntimeException.class,
errorHandling::handleErrorByThrowingAnyUncheckedException);
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingAnyUncheckedException());
}

@Ignore("Remove to run test")
@Test
public void testThrowAnyUncheckedExceptionWithDetailMessage() {
RuntimeException expected =
assertThrows(
RuntimeException.class,
() -> errorHandling
.handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage(
"This is the detail message."));
assertThat(expected).hasMessage("This is the detail message.");
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage(
"This is the detail message."))
.withMessage("This is the detail message.");
}

@Ignore("Remove to run test")
@Test
public void testThrowCustomCheckedException() {
assertThrows(
CustomCheckedException.class,
errorHandling::handleErrorByThrowingCustomCheckedException);
assertThatExceptionOfType(CustomCheckedException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomCheckedException());
}

@Ignore("Remove to run test")
@Test
public void testThrowCustomCheckedExceptionWithDetailMessage() {
CustomCheckedException expected =
assertThrows(
CustomCheckedException.class,
() -> errorHandling
.handleErrorByThrowingCustomCheckedExceptionWithDetailMessage(
"This is the detail message."));
assertThat(expected).hasMessage("This is the detail message.");
assertThatExceptionOfType(CustomCheckedException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomCheckedExceptionWithDetailMessage(
"This is the detail message."))
.withMessage("This is the detail message.");
}

@Ignore("Remove to run test")
@Test
public void testThrowCustomUncheckedException() {
assertThrows(
CustomUncheckedException.class,
errorHandling::handleErrorByThrowingCustomUncheckedException);
assertThatExceptionOfType(CustomUncheckedException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomUncheckedException());
}

@Ignore("Remove to run test")
@Test
public void testThrowCustomUncheckedExceptionWithDetailMessage() {
CustomUncheckedException expected =
assertThrows(
CustomUncheckedException.class,
() -> errorHandling
.handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage(
"This is the detail message."));
assertThat(expected).hasMessage("This is the detail message.");
assertThatExceptionOfType(CustomUncheckedException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage(
"This is the detail message."))
.withMessage("This is the detail message.");
}

@Ignore("Remove to run test")
@Test
public void testReturnOptionalInstance() {
Optional<Integer> successfulResult = errorHandling.handleErrorByReturningOptionalInstance("1");
assertTrue(successfulResult.isPresent());
assertEquals(1, (int) successfulResult.get());
assertThat(successfulResult).isPresent().hasValue(1);

Optional<Integer> failureResult = errorHandling.handleErrorByReturningOptionalInstance("a");
assertFalse(failureResult.isPresent());
assertThat(failureResult).isNotPresent();

}

}