Skip to content

Commit 529b75a

Browse files
keszoczeOliver Keszöczesanderploegsma
authored
Switch to assertj for exceptions (#2502)
* make error handling use AssertJ for exceptions * Adhere to coding style * Completely switch to AssertJ * make import explicit * Switch to AssertJ in n-th prime * Use AssertJ in the forth exercise * Use AssertJ in the ledger exercise * Use AssertJ in the OCR exercise * Use AssertJ in the palindrom calculator exercise * Use AssertJ in the phone number exercise * Use AssertJ in sgf parsing exercise * Use AssertJ in simple linked list exercise * Use AssertJ in triangle exercise * Add missing paranthesis * Use AssertJ in Custom Set exercise * Use AssertJ in Leap exercise * Use AssertJ in Luhn Validator exercise * Use AssertJ in Bracket Checker exercise * Use AssertJ in Pangram Checker exercise * Use AssertJ in Parallel Letter Frequency exercise * Use AssertJ in Queen Attack Calculator exercise * Update exercises/practice/sgf-parsing/src/test/java/SgfParsingTest.java Co-authored-by: Sander Ploegsma <[email protected]> * Update exercises/practice/sgf-parsing/src/test/java/SgfParsingTest.java Co-authored-by: Sander Ploegsma <[email protected]> * Remove debug output * Remove BDD-style comments * be more verbose in tests --------- Co-authored-by: Oliver Keszöcze <[email protected]> Co-authored-by: Sander Ploegsma <[email protected]>
1 parent eca5f62 commit 529b75a

File tree

17 files changed

+447
-723
lines changed

17 files changed

+447
-723
lines changed

exercises/practice/custom-set/src/test/java/CustomSetTest.java

Lines changed: 66 additions & 64 deletions
Large diffs are not rendered by default.

exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import static org.assertj.core.api.Assertions.*;
1+
import static org.assertj.core.api.Assertions.assertThat;
2+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
23

34
import org.junit.Ignore;
45
import org.junit.Test;

exercises/practice/forth/src/test/java/ForthEvaluatorTest.java

Lines changed: 100 additions & 237 deletions
Large diffs are not rendered by default.

exercises/practice/leap/src/test/java/LeapTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import org.junit.Ignore;
33
import org.junit.Test;
44

5-
import static org.junit.Assert.assertFalse;
6-
import static org.junit.Assert.assertTrue;
5+
import static org.assertj.core.api.Assertions.assertThat;
76

87
public class LeapTest {
98

@@ -16,55 +15,55 @@ public void setup() {
1615

1716
@Test
1817
public void testYearNotDivBy4InCommonYear() {
19-
assertFalse(leap.isLeapYear(2015));
18+
assertThat(leap.isLeapYear(2015)).isFalse();
2019
}
2120

2221
@Ignore("Remove to run test")
2322
@Test
2423
public void testYearDivBy2NotDivBy4InCommonYear() {
25-
assertFalse(leap.isLeapYear(1970));
24+
assertThat(leap.isLeapYear(1970)).isFalse();
2625
}
2726

2827
@Ignore("Remove to run test")
2928
@Test
3029
public void testYearDivBy4NotDivBy100InLeapYear() {
31-
assertTrue(leap.isLeapYear(1996));
30+
assertThat(leap.isLeapYear(1996)).isTrue();
3231
}
3332

3433
@Ignore("Remove to run test")
3534
@Test
3635
public void testYearDivBy4And5InLeapYear() {
37-
assertTrue(leap.isLeapYear(1960));
36+
assertThat(leap.isLeapYear(1960)).isTrue();
3837
}
3938

4039
@Ignore("Remove to run test")
4140
@Test
4241
public void testYearDivBy100NotDivBy400InCommonYear() {
43-
assertFalse(leap.isLeapYear(2100));
42+
assertThat(leap.isLeapYear(2100)).isFalse();
4443
}
4544

4645
@Ignore("Remove to run test")
4746
@Test
4847
public void testYearDivBy100NotDivBy3IsNotLeapYear() {
49-
assertFalse(leap.isLeapYear(1900));
48+
assertThat(leap.isLeapYear(1900)).isFalse();
5049
}
5150

5251
@Ignore("Remove to run test")
5352
@Test
5453
public void testYearDivBy400InLeapYear() {
55-
assertTrue(leap.isLeapYear(2000));
54+
assertThat(leap.isLeapYear(2000)).isTrue();
5655
}
5756

5857
@Ignore("Remove to run test")
5958
@Test
6059
public void testYearDivBy400NotDivBy125IsLeapYear() {
61-
assertTrue(leap.isLeapYear(2400));
60+
assertThat(leap.isLeapYear(2400)).isTrue();
6261
}
6362

6463
@Ignore("Remove to run test")
6564
@Test
6665
public void testYearDivBy200NotDivBy400InCommonYear() {
67-
assertFalse(leap.isLeapYear(1800));
66+
assertThat(leap.isLeapYear(1800)).isFalse();
6867
}
6968

7069
}
Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import org.junit.Assert;
21
import org.junit.Before;
32
import org.junit.Ignore;
43
import org.junit.Test;
54

6-
import static org.junit.Assert.assertEquals;
7-
import static org.junit.Assert.assertThrows;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
7+
88

99
public class LedgerTest {
1010

@@ -24,232 +24,188 @@ public void setUp() throws Exception {
2424
@Ignore("Remove to run test")
2525
@Test
2626
public void emptyLedgerUS() {
27-
// given
2827
var entries = new Ledger.LedgerEntry[]{};
2928

30-
// when
3129
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);
3230

33-
// then
3431
String expected = "Date | Description | Change ";
35-
Assert.assertEquals(expected, actual);
32+
assertThat(actual).isEqualTo(expected);
3633
}
3734

3835
@Ignore("Remove to run test")
3936
@Test
4037
public void emptyLedgerNL() {
41-
// given
4238
var entries = new Ledger.LedgerEntry[]{};
4339

44-
// when
4540
String actual = ledger.format(EUR_CURRENCY, NL_LOCALE, entries);
4641

47-
// then
4842
String expected = "Datum | Omschrijving | Verandering ";
49-
Assert.assertEquals(expected, actual);
43+
assertThat(actual).isEqualTo(expected);
5044
}
5145

5246
@Ignore("Remove to run test")
5347
@Test
5448
public void oneEntry() {
55-
// given
5649
var entries = new Ledger.LedgerEntry[]{
5750
ledger.createLedgerEntry("2015-02-01", "Buy present", -10.00)
5851
};
5952

60-
// when
6153
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);
6254

63-
// then
6455
String expected = "Date | Description | Change \n"
6556
+ "02/01/2015 | Buy present | -$10.00";
66-
Assert.assertEquals(expected, actual);
57+
assertThat(actual).isEqualTo(expected);
6758
}
6859

6960
@Ignore("Remove to run test")
7061
@Test
7162
public void creditAndDebit() {
72-
// given
7363
var entries = new Ledger.LedgerEntry[]{
7464
ledger.createLedgerEntry("2015-01-02", "Get present", 10.00),
7565
ledger.createLedgerEntry("2015-01-01", "Buy present", -10.00)
7666
};
7767

78-
// when
7968
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);
8069

81-
// then
8270
String expected = "Date | Description | Change \n" +
8371
"01/01/2015 | Buy present | -$10.00\n" +
8472
"01/02/2015 | Get present | $10.00";
8573

86-
Assert.assertEquals(expected, actual);
74+
assertThat(actual).isEqualTo(expected);
8775
}
8876

8977
@Ignore("Remove to run test")
9078
@Test
9179
public void multipleEntriesOnSameDateOrderedByDescription() {
92-
// given
9380
var entries = new Ledger.LedgerEntry[]{
9481
ledger.createLedgerEntry("2015-01-01", "Get present", 10.00),
9582
ledger.createLedgerEntry("2015-01-01", "Buy present", -10.00)
9683
};
9784

98-
// when
9985
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);
10086

101-
// then
10287
String expected = "Date | Description | Change \n" +
10388
"01/01/2015 | Buy present | -$10.00\n" +
10489
"01/01/2015 | Get present | $10.00";
10590

106-
Assert.assertEquals(expected, actual);
91+
assertThat(actual).isEqualTo(expected);
10792
}
10893

10994
@Ignore("Remove to run test")
11095
@Test
11196
public void finalOrderTieBreakerIsChange() {
112-
// given
11397
var entries = new Ledger.LedgerEntry[]{
11498
ledger.createLedgerEntry("2015-01-01", "Something", 0),
11599
ledger.createLedgerEntry("2015-01-01", "Something", -0.01),
116100
ledger.createLedgerEntry("2015-01-01", "Something", 0.01)
117101
};
118102

119-
// when
120103
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);
121104

122-
// then
123105
String expected = "Date | Description | Change \n" +
124106
"01/01/2015 | Something | -$0.01\n" +
125107
"01/01/2015 | Something | $0.00\n" +
126108
"01/01/2015 | Something | $0.01";
127109
;
128-
129-
Assert.assertEquals(expected, actual);
110+
assertThat(actual).isEqualTo(expected);
130111
}
131112

132113
@Ignore("Remove to run test")
133114
@Test
134115
public void overlongDescriptions() {
135-
// given
136116
var entries = new Ledger.LedgerEntry[]{
137117
ledger.createLedgerEntry("2015-01-01", "Freude schoner Gotterfunken", -1234.56)
138118
};
139119

140-
// when
141120
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);
142121

143-
// then
144122
String expected = "Date | Description | Change \n" +
145123
"01/01/2015 | Freude schoner Gotterf... | -$1,234.56";
146124

147-
Assert.assertEquals(expected, actual);
125+
assertThat(actual).isEqualTo(expected);
148126
}
149127

150128
@Ignore("Remove to run test")
151129
@Test
152130
public void euros() {
153-
// given
154131
var entries = new Ledger.LedgerEntry[]{
155132
ledger.createLedgerEntry("2015-01-01", "Buy present", -10.00)
156133
};
157134

158-
// when
159135
String actual = ledger.format(EUR_CURRENCY, US_LOCALE, entries);
160136

161-
// then
162137
var expected = "Date | Description | Change \n" +
163138
"01/01/2015 | Buy present | -€10.00";
164139

165-
Assert.assertEquals(expected, actual);
140+
assertThat(actual).isEqualTo(expected);
166141
}
167142

168143
@Ignore("Remove to run test")
169144
@Test
170145
public void dutchLocale() {
171-
// given
172146
var entries = new Ledger.LedgerEntry[]{
173147
ledger.createLedgerEntry("2015-03-12", "Buy present", 1234.56)
174148
};
175149

176-
// when
177150
String actual = ledger.format(USD_CURRENCY, NL_LOCALE, entries);
178151

179-
// then
180152
var expected = "Datum | Omschrijving | Verandering \n" +
181153
"12/03/2015 | Buy present | $1.234,56";
182154

183-
Assert.assertEquals(expected, actual);
155+
assertThat(actual).isEqualTo(expected);
184156
}
185157

186158
@Ignore("Remove to run test")
187159
@Test
188160
public void dutchNegativeNumberWith3DigitsBeforeDecimalPoint() {
189-
// given
190161
var entries = new Ledger.LedgerEntry[]{
191162
ledger.createLedgerEntry("2015-03-12", "Buy present", -123.45)
192163
};
193164

194-
// when
195165
String actual = ledger.format(USD_CURRENCY, NL_LOCALE, entries);
196166

197-
// then
198167
var expected = "Datum | Omschrijving | Verandering \n" +
199168
"12/03/2015 | Buy present | -$123,45";
200169

201-
Assert.assertEquals(expected, actual);
170+
assertThat(actual).isEqualTo(expected);
202171
}
203172

204173
@Ignore("Remove to run test")
205174
@Test
206175
public void americanNegativeNumberWith3DigitsBeforeDecimalPoint() {
207176

208-
// given
209177
var entries = new Ledger.LedgerEntry[]{
210178
ledger.createLedgerEntry("2015-03-12", "Buy present", -123.45)
211179
};
212180

213-
// when
214181
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);
215182

216-
// then
217183
var expected = "Date | Description | Change \n" +
218184
"03/12/2015 | Buy present | -$123.45";
219185

220-
Assert.assertEquals(expected, actual);
186+
assertThat(actual).isEqualTo(expected);
221187
}
222188

223189
@Ignore("Remove to run test")
224190
@Test
225191
public void givenInvalidLocale_ThenException() {
226-
// given
227192
var entries = new Ledger.LedgerEntry[0];
228193
var locale = "it-IT";
229194

230-
// when
231-
IllegalArgumentException illegalArgumentException = assertThrows(
232-
IllegalArgumentException.class,
233-
() -> ledger.format(EUR_CURRENCY, locale, entries));
234-
235-
// then
236-
assertEquals("Invalid locale", illegalArgumentException.getMessage());
195+
assertThatExceptionOfType(IllegalArgumentException.class)
196+
.isThrownBy(() -> ledger.format(EUR_CURRENCY, locale, entries))
197+
.withMessage("Invalid locale");
237198

238199
}
239200

240201
@Ignore("Remove to run test")
241202
@Test
242203
public void givenInvalidCurrency_ThenException() {
243-
// given
244204
var entries = new Ledger.LedgerEntry[0];
245205
var currency = "Pieces o' Eight";
246206

247-
// when
248-
IllegalArgumentException illegalArgumentException = assertThrows(
249-
IllegalArgumentException.class,
250-
() -> ledger.format(currency, US_LOCALE, entries));
251-
252-
// then
253-
assertEquals("Invalid currency", illegalArgumentException.getMessage());
207+
assertThatExceptionOfType(IllegalArgumentException.class)
208+
.isThrownBy(() -> ledger.format(currency, US_LOCALE, entries))
209+
.withMessage("Invalid currency");
254210
}
255211
}

0 commit comments

Comments
 (0)