Skip to content

Commit 0b71d38

Browse files
author
orrym
committed
BAEL-1517: Editor Review changes
1 parent c900694 commit 0b71d38

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

‎testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,58 @@
33
import static org.assertj.core.api.Assertions.assertThatThrownBy;
44
import static org.assertj.core.api.Assertions.assertThat;
55
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
7+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
8+
import static org.assertj.core.api.Assertions.assertThatIOException;
9+
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
610
import static org.assertj.core.api.Assertions.catchThrowable;
711

12+
import java.io.IOException;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
816
import org.junit.Test;
917

1018
public class Java8StyleAssertions {
1119

1220
@Test
13-
public void whenDividingByZero_thenArithmeticException() {
21+
public void whenGettingOutOfBoundsItem_thenIndexOutOfBoundsException() {
1422
assertThatThrownBy(() -> {
15-
int numerator = 10;
16-
int denominator = 0;
17-
int quotient = numerator / denominator;
18-
}).isInstanceOf(ArithmeticException.class)
19-
.hasMessageContaining("/ by zero");
23+
ArrayList<String> myStringList = new ArrayList<String>(Arrays.asList("Strine one", "String two"));
24+
myStringList.get(2);
25+
}).isInstanceOf(IndexOutOfBoundsException.class)
26+
.hasMessageStartingWith("Index: 2")
27+
.hasMessageContaining("2")
28+
.hasMessageEndingWith("Size: 2")
29+
.hasMessageContaining("Index: 2, Size: 2")
30+
.hasMessage("Index: %s, Size: %s", 2, 2)
31+
.hasMessageMatching("Index: \\d+, Size: \\d+")
32+
.hasNoCause();
33+
}
2034

35+
@Test
36+
public void whenWrappingException_thenCauseInstanceOfWrappedExceptionType() {
37+
assertThatThrownBy(() -> {
38+
try {
39+
throw new IOException();
40+
} catch (IOException e) {
41+
throw new RuntimeException(e);
42+
}
43+
}).isInstanceOf(RuntimeException.class)
44+
.hasCauseInstanceOf(IOException.class)
45+
.hasStackTraceContaining("java.io.IOException");
46+
}
47+
48+
@Test
49+
public void whenDividingByZero_thenArithmeticException() {
2150
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
2251
int numerator = 10;
2352
int denominator = 0;
2453
int quotient = numerator / denominator;
2554
})
2655
.withMessageContaining("/ by zero");
2756

28-
// BDD style:
57+
// Alternatively:
2958

3059
// when
3160
Throwable thrown = catchThrowable(() -> {

0 commit comments

Comments
 (0)