Skip to content

Commit 7ad49a1

Browse files
authored
Updates tests to use assertThat instead of assertEquals (#2511)
* Updates tests to use assertThat instead of assertEquals * Fix style check * Revert deprecated exercise changes * Fix style check * Revert imports change
1 parent edb5725 commit 7ad49a1

File tree

22 files changed

+400
-396
lines changed

22 files changed

+400
-396
lines changed

exercises/practice/complex-numbers/src/test/java/ComplexNumberTest.java

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

4-
import static org.junit.Assert.assertEquals;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
import static org.assertj.core.api.Assertions.withPrecision;
56

67
public class ComplexNumberTest {
78

@@ -12,7 +13,7 @@ public class ComplexNumberTest {
1213
private void assertDoublesEqual(double d1, double d2, String numberPart) {
1314
String errorMessage = "While testing " + numberPart + " part of number,";
1415

15-
assertEquals(errorMessage, d1, d2, DOUBLE_EQUALITY_TOLERANCE);
16+
assertThat(d1).withFailMessage(errorMessage).isCloseTo(d2, withPrecision(DOUBLE_EQUALITY_TOLERANCE));
1617
}
1718

1819
private void assertComplexNumbersEqual(ComplexNumber c1, ComplexNumber c2) {

exercises/practice/etl/src/test/java/EtlTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import org.junit.Test;
21
import org.junit.Ignore;
2+
import org.junit.Test;
33

4-
import java.util.*;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
59

6-
import static org.junit.Assert.assertEquals;
10+
import static org.assertj.core.api.Assertions.assertThat;
711

812

913
public class EtlTest {
@@ -25,7 +29,7 @@ public void testTransformOneValue() {
2529
};
2630
expected = Collections.unmodifiableMap(expected);
2731

28-
assertEquals(expected, etl.transform(old));
32+
assertThat(etl.transform(old)).isEqualTo(expected);
2933
}
3034

3135
@Ignore("Remove to run test")
@@ -49,7 +53,7 @@ public void testTransformMoreValues() {
4953
};
5054
expected = Collections.unmodifiableMap(expected);
5155

52-
assertEquals(expected, etl.transform(old));
56+
assertThat(etl.transform(old)).isEqualTo(expected);
5357
}
5458

5559
@Ignore("Remove to run test")
@@ -73,7 +77,7 @@ public void testMoreKeys() {
7377
};
7478
expected = Collections.unmodifiableMap(expected);
7579

76-
assertEquals(expected, etl.transform(old));
80+
assertThat(etl.transform(old)).isEqualTo(expected);
7781
}
7882

7983
@Ignore("Remove to run test")
@@ -124,6 +128,6 @@ public void testFullDataset() {
124128
};
125129
expected = Collections.unmodifiableMap(expected);
126130

127-
assertEquals(expected, etl.transform(old));
131+
assertThat(etl.transform(old)).isEqualTo(expected);
128132
}
129133
}
Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import org.junit.Before;
2-
import org.junit.Test;
32
import org.junit.Ignore;
3+
import org.junit.Test;
44

55
import static java.util.Arrays.asList;
6-
import static java.util.Collections.emptyList;
76
import static java.util.Collections.singletonList;
8-
import static org.junit.Assert.assertEquals;
7+
import static org.assertj.core.api.Assertions.assertThat;
98

109
public class FlattenerTest {
1110

@@ -18,67 +17,63 @@ public void setUp() {
1817

1918
@Test
2019
public void testFlatListIsPreserved() {
21-
assertEquals(asList(0, '1', "two"), flattener.flatten(asList(0, '1', "two")));
20+
assertThat(flattener.flatten(asList(0, '1', "two")))
21+
.containsExactly(0, '1', "two");
2222
}
2323

2424
@Ignore("Remove to run test")
2525
@Test
2626
public void testASingleLevelOfNestingWithNoNulls() {
27-
assertEquals(
28-
asList(1, '2', 3, 4, 5, "six", "7", 8),
29-
flattener.flatten(asList(1, asList('2', 3, 4, 5, "six", "7"), 8)));
27+
assertThat(flattener.flatten(asList(1, asList('2', 3, 4, 5, "six", "7"), 8)))
28+
.containsExactly(1, '2', 3, 4, 5, "six", "7", 8);
3029
}
3130

3231
@Ignore("Remove to run test")
3332
@Test
3433
public void testFiveLevelsOfNestingWithNoNulls() {
35-
assertEquals(
36-
asList(0, '2', 2, "three", '8', 100, "four", 50, "-2"),
37-
flattener.flatten(asList(0,
34+
assertThat(flattener.flatten(asList(0,
3835
'2',
3936
asList(asList(2, "three"),
4037
'8',
4138
100,
4239
"four",
43-
singletonList(singletonList(singletonList(50)))), "-2")));
40+
singletonList(singletonList(singletonList(50)))), "-2")))
41+
.containsExactly(0, '2', 2, "three", '8', 100, "four", 50, "-2");
4442
}
4543

4644
@Ignore("Remove to run test")
4745
@Test
4846
public void testSixLevelsOfNestingWithNoNulls() {
49-
assertEquals(
50-
asList("one", '2', 3, '4', 5, "six", 7, "8"),
51-
flattener.flatten(asList("one",
52-
asList('2',
47+
assertThat(flattener.flatten(asList("one",
48+
asList('2',
5349
singletonList(singletonList(3)),
54-
asList('4',
55-
singletonList(singletonList(5))), "six", 7), "8")));
50+
asList('4',
51+
singletonList(singletonList(5))), "six", 7), "8")))
52+
.containsExactly("one", '2', 3, '4', 5, "six", 7, "8");
5653
}
5754

5855
@Ignore("Remove to run test")
5956
@Test
6057
public void testSixLevelsOfNestingWithNulls() {
61-
assertEquals(
62-
asList("0", 2, "two", '3', "8", "one hundred", "negative two"),
63-
flattener.flatten(asList("0",
58+
assertThat(flattener.flatten(asList("0",
6459
2,
6560
asList(asList("two", '3'),
6661
"8",
6762
singletonList(singletonList("one hundred")),
68-
null,
69-
singletonList(singletonList(null))),
70-
"negative two")));
63+
null,
64+
singletonList(singletonList(null))),
65+
"negative two")))
66+
.containsExactly("0", 2, "two", '3', "8", "one hundred", "negative two");
7167
}
7268

7369
@Ignore("Remove to run test")
7470
@Test
7571
public void testNestedListsFullOfNullsOnly() {
76-
assertEquals(emptyList(),
77-
flattener.flatten(asList(null,
72+
assertThat(flattener.flatten(asList(null,
7873
singletonList(singletonList(singletonList(null))),
79-
null,
80-
null,
81-
asList(asList(null, null), null), null)));
74+
null,
75+
null,
76+
asList(asList(null, null), null), null))).isEmpty();
8277
}
8378

8479
}

exercises/practice/food-chain/src/test/java/FoodChainTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import org.junit.Before;
2-
import org.junit.Test;
32
import org.junit.Ignore;
3+
import org.junit.Test;
44

5-
import static org.junit.Assert.assertEquals;
5+
import static org.assertj.core.api.Assertions.assertThat;
66

77
public class FoodChainTest {
88
private FoodChain foodChain;
@@ -18,7 +18,7 @@ public void fly() {
1818
String expected = "I know an old lady who swallowed a fly.\n" +
1919
"I don't know why she swallowed the fly. Perhaps she'll die.";
2020

21-
assertEquals(expected, foodChain.verse(verse));
21+
assertThat(foodChain.verse(verse)).isEqualTo(expected);
2222
}
2323

2424
@Test
@@ -30,7 +30,7 @@ public void spider() {
3030
"She swallowed the spider to catch the fly.\n" +
3131
"I don't know why she swallowed the fly. Perhaps she'll die.";
3232

33-
assertEquals(expected, foodChain.verse(verse));
33+
assertThat(foodChain.verse(verse)).isEqualTo(expected);
3434
}
3535

3636
@Test
@@ -44,7 +44,7 @@ public void bird() {
4444
"She swallowed the spider to catch the fly.\n" +
4545
"I don't know why she swallowed the fly. Perhaps she'll die.";
4646

47-
assertEquals(expected, foodChain.verse(verse));
47+
assertThat(foodChain.verse(verse)).isEqualTo(expected);
4848
}
4949

5050
@Test
@@ -59,7 +59,7 @@ public void cat() {
5959
"She swallowed the spider to catch the fly.\n" +
6060
"I don't know why she swallowed the fly. Perhaps she'll die.";
6161

62-
assertEquals(expected, foodChain.verse(verse));
62+
assertThat(foodChain.verse(verse)).isEqualTo(expected);
6363
}
6464

6565

@@ -76,7 +76,7 @@ public void dog() {
7676
"She swallowed the spider to catch the fly.\n" +
7777
"I don't know why she swallowed the fly. Perhaps she'll die.";
7878

79-
assertEquals(expected, foodChain.verse(verse));
79+
assertThat(foodChain.verse(verse)).isEqualTo(expected);
8080
}
8181

8282
@Test
@@ -93,7 +93,7 @@ public void goat() {
9393
"She swallowed the spider to catch the fly.\n" +
9494
"I don't know why she swallowed the fly. Perhaps she'll die.";
9595

96-
assertEquals(expected, foodChain.verse(verse));
96+
assertThat(foodChain.verse(verse)).isEqualTo(expected);
9797
}
9898

9999
@Test
@@ -111,7 +111,7 @@ public void cow() {
111111
"She swallowed the spider to catch the fly.\n" +
112112
"I don't know why she swallowed the fly. Perhaps she'll die.";
113113

114-
assertEquals(expected, foodChain.verse(verse));
114+
assertThat(foodChain.verse(verse)).isEqualTo(expected);
115115
}
116116

117117
@Test
@@ -121,7 +121,7 @@ public void horse() {
121121
String expected = "I know an old lady who swallowed a horse.\n" +
122122
"She's dead, of course!";
123123

124-
assertEquals(expected, foodChain.verse(verse));
124+
assertThat(foodChain.verse(verse)).isEqualTo(expected);
125125
}
126126

127127

@@ -145,7 +145,7 @@ public void multipleVerses() {
145145
"She swallowed the spider to catch the fly.\n" +
146146
"I don't know why she swallowed the fly. Perhaps she'll die.";
147147

148-
assertEquals(expected, foodChain.verses(startVerse, endVerse));
148+
assertThat(foodChain.verses(startVerse, endVerse)).isEqualTo(expected);
149149
}
150150

151151

@@ -210,6 +210,6 @@ public void wholeSong() {
210210
"I know an old lady who swallowed a horse.\n" +
211211
"She's dead, of course!";
212212

213-
assertEquals(expected, foodChain.verses(startVerse, endVerse));
213+
assertThat(foodChain.verses(startVerse, endVerse)).isEqualTo(expected);
214214
}
215215
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
import org.junit.Test;
21
import org.junit.Ignore;
2+
import org.junit.Test;
33

44
import java.time.LocalDate;
55
import java.time.LocalDateTime;
66
import java.time.Month;
77

8-
import static org.junit.Assert.assertEquals;
8+
import static org.assertj.core.api.Assertions.assertThat;
99

1010
public class GigasecondTest {
1111

1212
@Test
1313
public void modernTime() {
1414
Gigasecond gigaSecond = new Gigasecond(LocalDate.of(2011, Month.APRIL, 25));
1515

16-
assertEquals(LocalDateTime.of(2043, Month.JANUARY, 1, 1, 46, 40), gigaSecond.getDateTime());
16+
assertThat(gigaSecond.getDateTime()).isEqualTo(LocalDateTime.of(2043, Month.JANUARY, 1, 1, 46, 40));
1717
}
1818

1919
@Ignore("Remove to run test")
2020
@Test
2121
public void afterEpochTime() {
2222
Gigasecond gigaSecond = new Gigasecond(LocalDate.of(1977, Month.JUNE, 13));
2323

24-
assertEquals(LocalDateTime.of(2009, Month.FEBRUARY, 19, 1, 46, 40), gigaSecond.getDateTime());
24+
assertThat(gigaSecond.getDateTime()).isEqualTo(LocalDateTime.of(2009, Month.FEBRUARY, 19, 1, 46, 40));
2525
}
2626

2727
@Ignore("Remove to run test")
2828
@Test
2929
public void beforeEpochTime() {
3030
Gigasecond gigaSecond = new Gigasecond(LocalDate.of(1959, Month.JULY, 19));
3131

32-
assertEquals(LocalDateTime.of(1991, Month.MARCH, 27, 1, 46, 40), gigaSecond.getDateTime());
32+
assertThat(gigaSecond.getDateTime()).isEqualTo(LocalDateTime.of(1991, Month.MARCH, 27, 1, 46, 40));
3333
}
3434

3535
@Ignore("Remove to run test")
3636
@Test
3737
public void withFullTimeSpecified() {
3838
Gigasecond gigaSecond = new Gigasecond(LocalDateTime.of(2015, Month.JANUARY, 24, 22, 0, 0));
3939

40-
assertEquals(LocalDateTime.of(2046, Month.OCTOBER, 2, 23, 46, 40), gigaSecond.getDateTime());
40+
assertThat(gigaSecond.getDateTime()).isEqualTo(LocalDateTime.of(2046, Month.OCTOBER, 2, 23, 46, 40));
4141
}
4242

4343
@Ignore("Remove to run test")
4444
@Test
4545
public void withFullTimeSpecifiedAndDayRollover() {
4646
Gigasecond gigaSecond = new Gigasecond(LocalDateTime.of(2015, Month.JANUARY, 24, 23, 59, 59));
4747

48-
assertEquals(LocalDateTime.of(2046, Month.OCTOBER, 3, 1, 46, 39), gigaSecond.getDateTime());
48+
assertThat(gigaSecond.getDateTime()).isEqualTo(LocalDateTime.of(2046, Month.OCTOBER, 3, 1, 46, 39));
4949
}
5050
}

0 commit comments

Comments
 (0)