Skip to content

Commit e8e997f

Browse files
authored
Convert exception assertions to AssertJ in QueenAttackCalculatorTest (exercism#2199)
1 parent 31eba37 commit e8e997f

1 file changed

Lines changed: 22 additions & 47 deletions

File tree

exercises/practice/queen-attack/src/test/java/QueenAttackCalculatorTest.java

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import static org.assertj.core.api.Assertions.assertThat;
2-
import static org.junit.Assert.assertFalse;
3-
import static org.junit.Assert.assertThrows;
4-
import static org.junit.Assert.assertTrue;
5-
61
import org.junit.Ignore;
72
import org.junit.Test;
83

4+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertTrue;
7+
98

109
public class QueenAttackCalculatorTest {
1110

@@ -17,49 +16,33 @@ public void testCreateQueenWithAValidPosition() {
1716
@Ignore("Remove to run test")
1817
@Test
1918
public void testCreateQueenMustHavePositiveRow() {
20-
IllegalArgumentException expected =
21-
assertThrows(
22-
IllegalArgumentException.class,
23-
() -> new Queen(-2, 2));
24-
25-
assertThat(expected)
26-
.hasMessage("Queen position must have positive row.");
19+
assertThatExceptionOfType(IllegalArgumentException.class)
20+
.isThrownBy(() -> new Queen(-2, 2))
21+
.withMessage("Queen position must have positive row.");
2722
}
2823

2924
@Ignore("Remove to run test")
3025
@Test
3126
public void testCreateQueenMustHaveRowOnBoard() {
32-
IllegalArgumentException expected =
33-
assertThrows(
34-
IllegalArgumentException.class,
35-
() -> new Queen(8, 4));
36-
37-
assertThat(expected)
38-
.hasMessage("Queen position must have row <= 7.");
27+
assertThatExceptionOfType(IllegalArgumentException.class)
28+
.isThrownBy(() -> new Queen(8, 4))
29+
.withMessage("Queen position must have row <= 7.");
3930
}
4031

4132
@Ignore("Remove to run test")
4233
@Test
4334
public void testCreateQueenMustHavePositiveColumn() {
44-
IllegalArgumentException expected =
45-
assertThrows(
46-
IllegalArgumentException.class,
47-
() -> new Queen(2, -2));
48-
49-
assertThat(expected)
50-
.hasMessage("Queen position must have positive column.");
35+
assertThatExceptionOfType(IllegalArgumentException.class)
36+
.isThrownBy(() -> new Queen(2, -2))
37+
.withMessage("Queen position must have positive column.");
5138
}
5239

5340
@Ignore("Remove to run test")
5441
@Test
5542
public void testCreateQueenMustHaveColumnOnBoard() {
56-
IllegalArgumentException expected =
57-
assertThrows(
58-
IllegalArgumentException.class,
59-
() -> new Queen(4, 8));
60-
61-
assertThat(expected)
62-
.hasMessage("Queen position must have column <= 7.");
43+
assertThatExceptionOfType(IllegalArgumentException.class)
44+
.isThrownBy(() -> new Queen(4, 8))
45+
.withMessage("Queen position must have column <= 7.");
6346
}
6447

6548
@Ignore("Remove to run test")
@@ -128,25 +111,17 @@ public void testQueensCanAttackOnFourthDiagonal() {
128111
@Ignore("Remove to run test")
129112
@Test
130113
public void testNullPositionsNotAllowed() {
131-
IllegalArgumentException expected =
132-
assertThrows(
133-
IllegalArgumentException.class,
134-
() -> new QueenAttackCalculator(null, new Queen(0, 7)));
135-
136-
assertThat(expected)
137-
.hasMessage("You must supply valid positions for both Queens.");
114+
assertThatExceptionOfType(IllegalArgumentException.class)
115+
.isThrownBy(() -> new QueenAttackCalculator(null, new Queen(0, 7)))
116+
.withMessage("You must supply valid positions for both Queens.");
138117
}
139118

140119
@Ignore("Remove to run test")
141120
@Test
142121
public void testQueensMustNotOccupyTheSameSquare() {
143-
IllegalArgumentException expected =
144-
assertThrows(
145-
IllegalArgumentException.class,
146-
() -> new QueenAttackCalculator(new Queen(2, 2), new Queen(2, 2)));
147-
148-
assertThat(expected)
149-
.hasMessage("Queens cannot occupy the same position.");
122+
assertThatExceptionOfType(IllegalArgumentException.class)
123+
.isThrownBy(() -> new QueenAttackCalculator(new Queen(2, 2), new Queen(2, 2)))
124+
.withMessage("Queens cannot occupy the same position.");
150125
}
151126

152127
}

0 commit comments

Comments
 (0)