Skip to content

Commit 84f99ff

Browse files
authored
Convert exception assertions to AssertJ in 2 Exercises (exercism#2176)
1 parent 76a5f6e commit 84f99ff

2 files changed

Lines changed: 23 additions & 48 deletions

File tree

exercises/practice/dominoes/src/test/java/DominoesTest.java

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import static org.assertj.core.api.Assertions.assertThat;
1+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22
import static org.junit.Assert.assertEquals;
3-
import static org.junit.Assert.assertThrows;
43

54
import org.junit.Ignore;
65
import org.junit.Test;
@@ -44,12 +43,9 @@ public void singletonCantBeChainedTest() {
4443
Domino[] dominoesArray = {new Domino(1, 2)};
4544
List<Domino> dominoesList = Arrays.asList(dominoesArray);
4645

47-
ChainNotFoundException expected =
48-
assertThrows(
49-
ChainNotFoundException.class,
50-
() -> dominoes.formChain(dominoesList));
51-
52-
assertThat(expected).hasMessage("No domino chain found.");
46+
assertThatExceptionOfType(ChainNotFoundException.class)
47+
.isThrownBy(() -> dominoes.formChain(dominoesList))
48+
.withMessage("No domino chain found.");
5349
}
5450

5551
@Ignore("Remove to run test")
@@ -86,12 +82,9 @@ public void cantBeChainedTest() {
8682
Domino[] dominoesArray = {new Domino(1, 2), new Domino(4, 1), new Domino(2, 3)};
8783
List<Domino> dominoesList = Arrays.asList(dominoesArray);
8884

89-
ChainNotFoundException expected =
90-
assertThrows(
91-
ChainNotFoundException.class,
92-
() -> dominoes.formChain(dominoesList));
93-
94-
assertThat(expected).hasMessage("No domino chain found.");
85+
assertThatExceptionOfType(ChainNotFoundException.class)
86+
.isThrownBy(() -> dominoes.formChain(dominoesList))
87+
.withMessage("No domino chain found.");
9588
}
9689

9790
@Ignore("Remove to run test")
@@ -102,12 +95,9 @@ public void disconnectedSimpleTest() {
10295
Domino[] dominoesArray = {new Domino(1, 1), new Domino(2, 2)};
10396
List<Domino> dominoesList = Arrays.asList(dominoesArray);
10497

105-
ChainNotFoundException expected =
106-
assertThrows(
107-
ChainNotFoundException.class,
108-
() -> dominoes.formChain(dominoesList));
109-
110-
assertThat(expected).hasMessage("No domino chain found.");
98+
assertThatExceptionOfType(ChainNotFoundException.class)
99+
.isThrownBy(() -> dominoes.formChain(dominoesList))
100+
.withMessage("No domino chain found.");
111101
}
112102

113103
@Ignore("Remove to run test")
@@ -118,12 +108,9 @@ public void disconnectedDoubleLoopTest() {
118108
Domino[] dominoesArray = {new Domino(1, 2), new Domino(2, 1), new Domino(3, 4), new Domino(4, 3)};
119109
List<Domino> dominoesList = Arrays.asList(dominoesArray);
120110

121-
ChainNotFoundException expected =
122-
assertThrows(
123-
ChainNotFoundException.class,
124-
() -> dominoes.formChain(dominoesList));
125-
126-
assertThat(expected).hasMessage("No domino chain found.");
111+
assertThatExceptionOfType(ChainNotFoundException.class)
112+
.isThrownBy(() -> dominoes.formChain(dominoesList))
113+
.withMessage("No domino chain found.");
127114
}
128115

129116
@Ignore("Remove to run test")
@@ -134,12 +121,9 @@ public void disconnectedSingleIsolatedTest() {
134121
Domino[] dominoesArray = {new Domino(1, 2), new Domino(2, 3), new Domino(3, 1), new Domino(4, 4)};
135122
List<Domino> dominoesList = Arrays.asList(dominoesArray);
136123

137-
ChainNotFoundException expected =
138-
assertThrows(
139-
ChainNotFoundException.class,
140-
() -> dominoes.formChain(dominoesList));
141-
142-
assertThat(expected).hasMessage("No domino chain found.");
124+
assertThatExceptionOfType(ChainNotFoundException.class)
125+
.isThrownBy(() -> dominoes.formChain(dominoesList))
126+
.withMessage("No domino chain found.");
143127
}
144128

145129
@Ignore("Remove to run test")

exercises/practice/variable-length-quantity/src/test/java/VariableLengthQuantityTest.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import static org.assertj.core.api.Assertions.assertThat;
1+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22
import static org.junit.Assert.assertEquals;
3-
import static org.junit.Assert.assertThrows;
43

54
import org.junit.Ignore;
65
import org.junit.Test;
@@ -228,27 +227,19 @@ public void testDecodeMaximum32BitInteger() {
228227
public void testCannotDecodeIncompleteSequence() {
229228
List<Long> bytes = Arrays.asList(0xffL);
230229

231-
IllegalArgumentException expected =
232-
assertThrows(
233-
IllegalArgumentException.class,
234-
() -> variableLengthQuantity.decode(bytes));
235-
236-
assertThat(expected)
237-
.hasMessage("Invalid variable-length quantity encoding");
230+
assertThatExceptionOfType(IllegalArgumentException.class)
231+
.isThrownBy(() -> variableLengthQuantity.decode(bytes))
232+
.withMessage("Invalid variable-length quantity encoding");
238233
}
239234

240235
@Ignore("Remove to run test")
241236
@Test
242237
public void testCannotDecodeIncompleteSequenceEvenIfValueIsZero() {
243238
List<Long> bytes = Arrays.asList(0x80L);
244239

245-
IllegalArgumentException expected =
246-
assertThrows(
247-
IllegalArgumentException.class,
248-
() -> variableLengthQuantity.decode(bytes));
249-
250-
assertThat(expected)
251-
.hasMessage("Invalid variable-length quantity encoding");
240+
assertThatExceptionOfType(IllegalArgumentException.class)
241+
.isThrownBy(() -> variableLengthQuantity.decode(bytes))
242+
.withMessage("Invalid variable-length quantity encoding");
252243
}
253244

254245
@Ignore("Remove to run test")

0 commit comments

Comments
 (0)