Skip to content

Commit 9cc582e

Browse files
orrymrmaibin
authored andcommitted
BAEL-1517: Editor Review Changes (eugenp#3635)
* Add XML, JavaConfig and Autowired examples. * BAEL-1517: Added Java7 style assertions * BAEL-1517: Upgrade AssertJ to 3.9.0; add Java 8 style assertion tests * Revert "Add XML, JavaConfig and Autowired examples." This reverts commit 8f4df6b. * BAEL-1517: Editor Review changes * BAEL-1517: Formatting...
1 parent 71e27c6 commit 9cc582e

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,51 @@
55
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
66
import static org.assertj.core.api.Assertions.catchThrowable;
77

8+
import java.io.IOException;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
811
import org.junit.Test;
912

1013
public class Java8StyleAssertions {
1114

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

30+
@Test
31+
public void whenWrappingException_thenCauseInstanceOfWrappedExceptionType() {
32+
assertThatThrownBy(() -> {
33+
try {
34+
throw new IOException();
35+
} catch (IOException e) {
36+
throw new RuntimeException(e);
37+
}
38+
}).isInstanceOf(RuntimeException.class)
39+
.hasCauseInstanceOf(IOException.class)
40+
.hasStackTraceContaining("IOException");
41+
}
42+
43+
@Test
44+
public void whenDividingByZero_thenArithmeticException() {
2145
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
2246
int numerator = 10;
2347
int denominator = 0;
2448
int quotient = numerator / denominator;
2549
})
2650
.withMessageContaining("/ by zero");
2751

28-
// BDD style:
52+
// Alternatively:
2953

3054
// when
3155
Throwable thrown = catchThrowable(() -> {

0 commit comments

Comments
 (0)