Skip to content

Commit 31eba37

Browse files
authored
Convert exception assertions to AssertJ in 3 exercises (exercism#2194)
1 parent b415b67 commit 31eba37

3 files changed

Lines changed: 50 additions & 93 deletions

File tree

exercises/practice/circular-buffer/src/test/java/CircularBufferTest.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import static org.assertj.core.api.Assertions.assertThat;
2-
import static org.junit.Assert.assertThrows;
2+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
33

44
import org.junit.Ignore;
55
import org.junit.Test;
@@ -10,11 +10,9 @@ public class CircularBufferTest {
1010
public void readingFromEmptyBufferShouldThrowException() {
1111
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
1212

13-
BufferIOException expected =
14-
assertThrows(BufferIOException.class, buffer::read);
15-
16-
assertThat(expected)
17-
.hasMessage("Tried to read from empty buffer");
13+
assertThatExceptionOfType(BufferIOException.class)
14+
.isThrownBy(buffer::read)
15+
.withMessage("Tried to read from empty buffer");
1816
}
1917

2018
@Ignore("Remove to run test")
@@ -34,16 +32,14 @@ public void canReadItemOnlyOnce() throws BufferIOException {
3432
buffer.write(1);
3533
assertThat(buffer.read()).isEqualTo(1);
3634

37-
BufferIOException expected =
38-
assertThrows(BufferIOException.class, buffer::read);
39-
40-
assertThat(expected)
41-
.hasMessage("Tried to read from empty buffer");
35+
assertThatExceptionOfType(BufferIOException.class)
36+
.isThrownBy(buffer::read)
37+
.withMessage("Tried to read from empty buffer");
4238
}
4339

4440
@Ignore("Remove to run test")
4541
@Test
46-
public void readsItemsInOrderWritten() throws BufferIOException {
42+
public void readsItemsInOrderWritten() throws BufferIOException {
4743
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);
4844

4945
buffer.write(1);
@@ -59,11 +55,9 @@ public void fullBufferCantBeWrittenTo() throws BufferIOException {
5955

6056
buffer.write(1);
6157

62-
BufferIOException expected =
63-
assertThrows(BufferIOException.class, () -> buffer.write(2));
64-
65-
assertThat(expected)
66-
.hasMessage("Tried to write to full buffer");
58+
assertThatExceptionOfType(BufferIOException.class)
59+
.isThrownBy(() -> buffer.write(2))
60+
.withMessage("Tried to write to full buffer");
6761
}
6862

6963
@Ignore("Remove to run test")
@@ -98,11 +92,9 @@ public void cantReadClearedItems() throws BufferIOException {
9892
buffer.write(1);
9993
buffer.clear();
10094

101-
BufferIOException expected =
102-
assertThrows(BufferIOException.class, buffer::read);
103-
104-
assertThat(expected)
105-
.hasMessage("Tried to read from empty buffer");
95+
assertThatExceptionOfType(BufferIOException.class)
96+
.isThrownBy(buffer::read)
97+
.withMessage("Tried to read from empty buffer");
10698
}
10799

108100
@Ignore("Remove to run test")
@@ -178,11 +170,8 @@ public void initialClearDoesNotAffectWrappingAround() throws BufferIOException {
178170
assertThat(buffer.read()).isEqualTo(3);
179171
assertThat(buffer.read()).isEqualTo(4);
180172

181-
BufferIOException expected =
182-
assertThrows(BufferIOException.class, buffer::read);
183-
184-
assertThat(expected)
185-
.hasMessage("Tried to read from empty buffer");
173+
assertThatExceptionOfType(BufferIOException.class)
174+
.isThrownBy(buffer::read)
175+
.withMessage("Tried to read from empty buffer");
186176
}
187177
}
188-

exercises/practice/go-counting/src/test/java/GoCountingTest.java

Lines changed: 17 additions & 30 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;
@@ -13,10 +12,10 @@
1312
public class GoCountingTest {
1413

1514
String board5x5 = " B \n" +
16-
" B B \n" +
17-
"B W B\n" +
18-
" W W \n" +
19-
" W ";
15+
" B B \n" +
16+
"B W B\n" +
17+
" W W \n" +
18+
" W ";
2019

2120
@Test
2221
public void blackCorner5x5BoardTest() {
@@ -73,51 +72,39 @@ public void stoneNotTerritory5x5Board() {
7372
public void invalidXTooLow5x5Board() {
7473
GoCounting gocounting = new GoCounting(board5x5);
7574

76-
IllegalArgumentException expected =
77-
assertThrows(
78-
IllegalArgumentException.class,
79-
() -> gocounting.getTerritory(-1, 1));
80-
81-
assertThat(expected).hasMessage("Invalid coordinate");
75+
assertThatExceptionOfType(IllegalArgumentException.class)
76+
.isThrownBy(() -> gocounting.getTerritory(-1, 1))
77+
.withMessage("Invalid coordinate");
8278
}
8379

8480
@Ignore("Remove to run test")
8581
@Test
8682
public void invalidXTooHigh5x5Board() {
8783
GoCounting gocounting = new GoCounting(board5x5);
8884

89-
IllegalArgumentException expected =
90-
assertThrows(
91-
IllegalArgumentException.class,
92-
() -> gocounting.getTerritory(5, 1));
93-
94-
assertThat(expected).hasMessage("Invalid coordinate");
85+
assertThatExceptionOfType(IllegalArgumentException.class)
86+
.isThrownBy(() -> gocounting.getTerritory(5, 1))
87+
.withMessage("Invalid coordinate");
9588
}
9689

9790
@Ignore("Remove to run test")
9891
@Test
9992
public void invalidYTooLow5x5Board() {
10093
GoCounting gocounting = new GoCounting(board5x5);
10194

102-
IllegalArgumentException expected =
103-
assertThrows(
104-
IllegalArgumentException.class,
105-
() -> gocounting.getTerritory(1, -1));
106-
107-
assertThat(expected).hasMessage("Invalid coordinate");
95+
assertThatExceptionOfType(IllegalArgumentException.class)
96+
.isThrownBy(() -> gocounting.getTerritory(1, -1))
97+
.withMessage("Invalid coordinate");
10898
}
10999

110100
@Ignore("Remove to run test")
111101
@Test
112102
public void invalidYTooHigh5x5Board() {
113103
GoCounting gocounting = new GoCounting(board5x5);
114104

115-
IllegalArgumentException expected =
116-
assertThrows(
117-
IllegalArgumentException.class,
118-
() -> gocounting.getTerritory(1, 5));
119-
120-
assertThat(expected).hasMessage("Invalid coordinate");
105+
assertThatExceptionOfType(IllegalArgumentException.class)
106+
.isThrownBy(() -> gocounting.getTerritory(1, 5))
107+
.withMessage("Invalid coordinate");
121108
}
122109

123110
@Ignore("Remove to run test")

exercises/practice/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java

Lines changed: 16 additions & 35 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;
@@ -75,8 +74,8 @@ public void testCorrectlyCalculatesLargestProductOfLengthFiveWithNumbersInOrder(
7574
@Ignore("Remove to run test")
7675
@Test
7776
public void testCorrectlyCalculatesLargestProductInLongStringToSearchV1() {
78-
LargestSeriesProductCalculator calculator
79-
= new LargestSeriesProductCalculator("73167176531330624919225119674426574742355349194934");
77+
LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator(
78+
"73167176531330624919225119674426574742355349194934");
8079

8180
long expectedProduct = 23520;
8281

@@ -112,14 +111,9 @@ public void testCorrectlyCalculatesLargestProductOfZeroIfAllSeriesOfGivenLengthC
112111
public void testSeriesLengthLongerThanLengthOfStringToTestIsRejected() {
113112
LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("123");
114113

115-
IllegalArgumentException expected =
116-
assertThrows(
117-
IllegalArgumentException.class,
118-
() -> calculator.calculateLargestProductForSeriesLength(4));
119-
120-
assertThat(expected)
121-
.hasMessage(
122-
"Series length must be less than or equal to the length of the string to search.");
114+
assertThatExceptionOfType(IllegalArgumentException.class)
115+
.isThrownBy(() -> calculator.calculateLargestProductForSeriesLength(4))
116+
.withMessage("Series length must be less than or equal to the length of the string to search.");
123117
}
124118

125119
@Ignore("Remove to run test")
@@ -149,42 +143,29 @@ public void testCorrectlyCalculatesLargestProductOfLength0ForNonEmptyStringToSea
149143
public void testEmptyStringToSearchAndSeriesOfNonZeroLengthIsRejected() {
150144
LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("");
151145

152-
IllegalArgumentException expected =
153-
assertThrows(
154-
IllegalArgumentException.class,
155-
() -> calculator.calculateLargestProductForSeriesLength(1));
156-
157-
assertThat(expected)
158-
.hasMessage(
159-
"Series length must be less than or equal to the length of the string to search.");
146+
assertThatExceptionOfType(IllegalArgumentException.class)
147+
.isThrownBy(() -> calculator.calculateLargestProductForSeriesLength(1))
148+
.withMessage("Series length must be less than or equal to the length of the string to search.");
160149
}
161150

162151
@Ignore("Remove to run test")
163152
@Test
164153
public void testStringToSearchContainingNonDigitCharacterIsRejected() {
165-
IllegalArgumentException expected =
166-
assertThrows(
167-
IllegalArgumentException.class,
168-
() -> new LargestSeriesProductCalculator("1234a5"));
169-
170-
assertThat(expected)
171-
.hasMessage("String to search may only contain digits.");
154+
assertThatExceptionOfType(IllegalArgumentException.class)
155+
.isThrownBy(() -> new LargestSeriesProductCalculator("1234a5"))
156+
.withMessage("String to search may only contain digits.");
172157
}
173158

174159
@Ignore("Remove to run test")
175160
@Test
176161
public void testNegativeSeriesLengthIsRejected() {
177162
LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("12345");
178163

179-
IllegalArgumentException expected =
180-
assertThrows(
181-
IllegalArgumentException.class,
182-
() -> calculator.calculateLargestProductForSeriesLength(-1));
183-
184-
assertThat(expected)
185-
.hasMessage("Series length must be non-negative.");
164+
assertThatExceptionOfType(IllegalArgumentException.class)
165+
.isThrownBy(() -> calculator.calculateLargestProductForSeriesLength(-1))
166+
.withMessage("Series length must be non-negative.");
186167
}
187-
168+
188169
@Ignore("Remove to run test")
189170
@Test
190171
public void testForIntegerOverflow() {

0 commit comments

Comments
 (0)