Skip to content

Commit 145db05

Browse files
author
orrym
committed
BAEL-1517: Upgrade AssertJ to 3.9.0; add Java 8 style assertion tests
1 parent 4344479 commit 145db05

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎testing-modules/testing/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
<jacoco.version>0.7.7.201606060606</jacoco.version>
174174
<guava.version>21.0</guava.version>
175175
<assertj-guava.version>3.1.0</assertj-guava.version>
176-
<assertj-core.version>3.6.1</assertj-core.version>
176+
<assertj-core.version>3.9.0</assertj-core.version>
177177
<assertj-generator.version>2.1.0</assertj-generator.version>
178178
<truth.version>0.32</truth.version>
179179
<jUnitParams.version>1.1.0</jUnitParams.version>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ public void whenDividingByZero_thenArithmeticException() {
1515
int denominator = 0;
1616
int quotient = numerator / denominator;
1717
fail("ArithmeticException expected because dividing by zero yields an ArithmeticException.");
18-
failBecauseExceptionWasNotThrown(IndexOutOfBoundsException.class);
19-
} catch (ArithmeticException e) {
18+
failBecauseExceptionWasNotThrown(ArithmeticException.class);
19+
} catch (Exception e) {
2020
assertThat(e).hasMessage("/ by zero");
21+
assertThat(e).isInstanceOf(ArithmeticException.class);
2122
}
2223
}
2324
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.testing.assertj.exceptions;
2+
3+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6+
import static org.assertj.core.api.Assertions.catchThrowable;
7+
8+
import org.junit.Test;
9+
10+
public class Java8StyleAssertions {
11+
12+
@Test
13+
public void whenDividingByZero_thenArithmeticException() {
14+
assertThatThrownBy(() -> {
15+
int numerator = 10;
16+
int denominator = 0;
17+
int quotient = numerator / denominator;
18+
}).isInstanceOf(ArithmeticException.class)
19+
.hasMessageContaining("/ by zero");
20+
21+
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
22+
int numerator = 10;
23+
int denominator = 0;
24+
int quotient = numerator / denominator;
25+
})
26+
.withMessageContaining("/ by zero");
27+
28+
// BDD style:
29+
30+
// when
31+
Throwable thrown = catchThrowable(() -> {
32+
int numerator = 10;
33+
int denominator = 0;
34+
int quotient = numerator / denominator;
35+
});
36+
37+
// then
38+
assertThat(thrown).isInstanceOf(ArithmeticException.class)
39+
.hasMessageContaining("/ by zero");
40+
41+
}
42+
}

0 commit comments

Comments
 (0)