BAEL-1517: Editor Review changes
This commit is contained in:
parent
c9006942c2
commit
0b71d385f0
|
@ -3,21 +3,50 @@ package com.baeldung.testing.assertj.exceptions;
|
||||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatIOException;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
|
||||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class Java8StyleAssertions {
|
public class Java8StyleAssertions {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenDividingByZero_thenArithmeticException() {
|
public void whenGettingOutOfBoundsItem_thenIndexOutOfBoundsException() {
|
||||||
assertThatThrownBy(() -> {
|
assertThatThrownBy(() -> {
|
||||||
int numerator = 10;
|
ArrayList<String> myStringList = new ArrayList<String>(Arrays.asList("Strine one", "String two"));
|
||||||
int denominator = 0;
|
myStringList.get(2);
|
||||||
int quotient = numerator / denominator;
|
}).isInstanceOf(IndexOutOfBoundsException.class)
|
||||||
}).isInstanceOf(ArithmeticException.class)
|
.hasMessageStartingWith("Index: 2")
|
||||||
.hasMessageContaining("/ by zero");
|
.hasMessageContaining("2")
|
||||||
|
.hasMessageEndingWith("Size: 2")
|
||||||
|
.hasMessageContaining("Index: 2, Size: 2")
|
||||||
|
.hasMessage("Index: %s, Size: %s", 2, 2)
|
||||||
|
.hasMessageMatching("Index: \\d+, Size: \\d+")
|
||||||
|
.hasNoCause();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenWrappingException_thenCauseInstanceOfWrappedExceptionType() {
|
||||||
|
assertThatThrownBy(() -> {
|
||||||
|
try {
|
||||||
|
throw new IOException();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}).isInstanceOf(RuntimeException.class)
|
||||||
|
.hasCauseInstanceOf(IOException.class)
|
||||||
|
.hasStackTraceContaining("java.io.IOException");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenDividingByZero_thenArithmeticException() {
|
||||||
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
|
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
|
||||||
int numerator = 10;
|
int numerator = 10;
|
||||||
int denominator = 0;
|
int denominator = 0;
|
||||||
|
@ -25,7 +54,7 @@ public class Java8StyleAssertions {
|
||||||
})
|
})
|
||||||
.withMessageContaining("/ by zero");
|
.withMessageContaining("/ by zero");
|
||||||
|
|
||||||
// BDD style:
|
// Alternatively:
|
||||||
|
|
||||||
// when
|
// when
|
||||||
Throwable thrown = catchThrowable(() -> {
|
Throwable thrown = catchThrowable(() -> {
|
||||||
|
|
Loading…
Reference in New Issue