Skip to content

Commit d4071c2

Browse files
authored
Converting tests to AssertJ (exercises starting with H, I, K) (exercism#2052)
* Updating Hamming to use assertJ * Hangman - assertJ conversion, first pass * Updating tests to use better assertions. * Updating HelloWorld to use assertJ * Converting hexadecimal to use assertJ * Updating House tests to use assertJ * Switching IsbnVerificationTest to use assertJ * Converting IsogramCheckerTest to use assertJ * Converting KindergartenGardenTest to use AssertJ * Updating Knapsack and test to use AssertJ. Also updating reference implementation, removing unnecessary reference to ArrayList. * Removing duplicate import. * Removing unused import in HexadecimalTest. * Adjusting HangmanTest assertions * Updating formatting HouseTest
1 parent 65faa74 commit d4071c2

File tree

10 files changed

+417
-487
lines changed

10 files changed

+417
-487
lines changed

exercises/practice/hamming/src/test/java/HammingTest.java

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

54
import org.junit.Ignore;
@@ -9,31 +8,31 @@ public class HammingTest {
98

109
@Test
1110
public void testNoDistanceBetweenEmptyStrands() {
12-
assertEquals(0, new Hamming("", "").getHammingDistance());
11+
assertThat(new Hamming("", "").getHammingDistance()).isEqualTo(0);
1312
}
1413

1514
@Ignore("Remove to run test")
1615
@Test
1716
public void testNoDistanceBetweenShortIdenticalStrands() {
18-
assertEquals(0, new Hamming("A", "A").getHammingDistance());
17+
assertThat(new Hamming("A", "A").getHammingDistance()).isEqualTo(0);
1918
}
2019

2120
@Ignore("Remove to run test")
2221
@Test
2322
public void testCompleteDistanceInSingleLetterDifferentStrands() {
24-
assertEquals(1, new Hamming("G", "T").getHammingDistance());
23+
assertThat(new Hamming("G", "T").getHammingDistance()).isEqualTo(1);
2524
}
2625

2726
@Ignore("Remove to run test")
2827
@Test
2928
public void testDistanceInLongIdenticalStrands() {
30-
assertEquals(0, new Hamming("GGACTGAAATCTG", "GGACTGAAATCTG").getHammingDistance());
29+
assertThat(new Hamming("GGACTGAAATCTG", "GGACTGAAATCTG").getHammingDistance()).isEqualTo(0);
3130
}
3231

3332
@Ignore("Remove to run test")
3433
@Test
3534
public void testDistanceInLongDifferentStrands() {
36-
assertEquals(9, new Hamming("GGACGGATTCTG", "AGGACGGATTCT").getHammingDistance());
35+
assertThat(new Hamming("GGACGGATTCTG", "AGGACGGATTCT").getHammingDistance()).isEqualTo(9);
3736
}
3837

3938
@Ignore("Remove to run test")

exercises/practice/hangman/src/test/java/HangmanTest.java

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
import java.util.*;
1010
import java.util.stream.Stream;
11+
1112
import org.junit.rules.ExpectedException;
1213

13-
import static org.junit.Assert.assertEquals;
14-
import static org.junit.Assert.assertNotNull;
14+
import static org.assertj.core.api.Assertions.assertThat;
1515
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1616

1717
public class HangmanTest {
@@ -32,13 +32,14 @@ public void initialization() {
3232
Observable.fromArray("secret"),
3333
Observable.fromArray());
3434
Output init = result.blockingFirst();
35-
assertNotNull(init);
36-
assertEquals("secret", init.secret);
37-
assertEquals("______", init.discovered);
38-
assertEquals(Collections.emptySet(), init.guess);
39-
assertEquals(Collections.emptySet(), init.misses);
40-
assertEquals(Collections.emptyList(), init.parts);
41-
assertEquals(Status.PLAYING, init.status);
35+
36+
assertThat(init).isNotNull();
37+
assertThat(init.secret).isEqualTo("secret");
38+
assertThat(init.discovered).isEqualTo("______");
39+
assertThat(init.guess).isEmpty();
40+
assertThat(init.misses).isEmpty();
41+
assertThat(init.parts).isEmpty();
42+
assertThat(init.status).isEqualTo(Status.PLAYING);
4243
}
4344

4445
@Ignore("Remove to run test")
@@ -47,12 +48,13 @@ public void firstGuess() {
4748
Observable<Output> result = hangman.play(
4849
Observable.fromArray("secret"),
4950
Observable.fromArray("e"));
51+
5052
Output last = result.blockingLast();
51-
assertEquals("_e__e_", last.discovered);
52-
assertEquals(Collections.singleton("e"), last.guess);
53-
assertEquals(Collections.emptySet(), last.misses);
54-
assertEquals(Collections.emptyList(), last.parts);
55-
assertEquals(Status.PLAYING, last.status);
53+
assertThat(last.discovered).isEqualTo("_e__e_");
54+
assertThat(last.guess).containsExactly("e");
55+
assertThat(last.misses).isEmpty();
56+
assertThat(last.parts).isEmpty();
57+
assertThat(last.status).isEqualTo(Status.PLAYING);
5658
}
5759

5860
@Ignore("Remove to run test")
@@ -62,11 +64,12 @@ public void firstMiss() {
6264
Observable.fromArray("secret"),
6365
Observable.fromArray("a"));
6466
Output last = result.blockingLast();
65-
assertEquals("______", last.discovered);
66-
assertEquals(Collections.emptySet(), last.guess);
67-
assertEquals(Collections.singleton("a"), last.misses);
68-
assertEquals(Collections.singletonList(Part.HEAD), last.parts);
69-
assertEquals(Status.PLAYING, last.status);
67+
68+
assertThat(last.discovered).isEqualTo("______");
69+
assertThat(last.guess).isEmpty();
70+
assertThat(last.misses).containsExactly("a");
71+
assertThat(last.parts).containsExactly(Part.HEAD);
72+
assertThat(last.status).isEqualTo(Status.PLAYING);
7073
}
7174

7275
@Ignore("Remove to run test")
@@ -76,15 +79,12 @@ public void gameInProgress() {
7679
Observable.fromArray("secret"),
7780
Observable.fromArray("a", "e", "o", "s"));
7881
Output last = result.blockingLast();
79-
assertEquals("se__e_", last.discovered);
80-
assertEquals(Set.of("e", "s"), last.guess);
81-
assertEquals(Set.of("a", "o"), last.misses);
82-
assertEquals(
83-
Arrays.asList(
84-
Part.HEAD,
85-
Part.BODY),
86-
last.parts);
87-
assertEquals(Status.PLAYING, last.status);
82+
83+
assertThat(last.discovered).isEqualTo("se__e_");
84+
assertThat(last.guess).containsExactlyInAnyOrder("e", "s");
85+
assertThat(last.misses).containsExactlyInAnyOrder("a", "o");
86+
assertThat(last.parts).containsExactlyInAnyOrder(Part.HEAD, Part.BODY);
87+
assertThat(last.status).isEqualTo(Status.PLAYING);
8888
}
8989

9090
@Ignore("Remove to run test")
@@ -94,9 +94,10 @@ public void wonGame() {
9494
Observable.fromArray("secret"),
9595
Observable.fromArray("a", "e", "o", "s", "c", "r", "g", "t"));
9696
Output last = result.blockingLast();
97-
assertEquals("secret", last.discovered);
98-
assertEquals(Set.of("c", "e", "r", "s", "t"), last.guess);
99-
assertEquals(Status.WIN, last.status);
97+
98+
assertThat(last.discovered).isEqualTo("secret");
99+
assertThat(last.guess).containsExactlyInAnyOrder("c", "e", "r", "s", "t");
100+
assertThat(last.status).isEqualTo(Status.WIN);
100101
}
101102

102103
@Ignore("Remove to run test")
@@ -106,18 +107,18 @@ public void lostGame() {
106107
Observable.fromArray("secret"),
107108
Observable.fromArray("a", "b", "c", "d", "e", "f", "g", "h"));
108109
Output last = result.blockingLast();
109-
assertEquals("_ec_e_", last.discovered);
110-
assertEquals(Set.of("a", "b", "d", "f", "g", "h"), last.misses);
111-
assertEquals(Status.LOSS, last.status);
112-
assertEquals(
113-
Arrays.asList(
110+
111+
assertThat(last.discovered).isEqualTo("_ec_e_");
112+
assertThat(last.misses).containsExactlyInAnyOrder("a", "b", "d", "f", "g", "h");
113+
assertThat(last.status).isEqualTo(Status.LOSS);
114+
assertThat(last.parts).containsExactlyInAnyOrder(
114115
Part.HEAD,
115116
Part.BODY,
116117
Part.LEFT_ARM,
117118
Part.RIGHT_ARM,
118119
Part.LEFT_LEG,
119-
Part.RIGHT_LEG),
120-
last.parts);
120+
Part.RIGHT_LEG
121+
);
121122
}
122123

123124
@Ignore("Remove to run test")
@@ -149,19 +150,19 @@ public void consecutiveGames() {
149150
Disposable subscription = outputs.filter(output -> output.status != Status.PLAYING)
150151
.subscribe(results::add);
151152
try {
152-
assertEquals(2, results.size());
153+
assertThat(results.size()).isEqualTo(2);
153154

154155
Output first = results.get(0);
155-
assertEquals("secret", first.discovered);
156-
assertEquals(Set.of("s", "e", "c", "r", "t"), first.guess);
157-
assertEquals(Set.of("a", "o", "g"), first.misses);
158-
assertEquals(Status.WIN, first.status);
156+
assertThat(first.discovered).isEqualTo("secret");
157+
assertThat(first.guess).containsExactlyInAnyOrder("s", "e", "c", "r", "t");
158+
assertThat(first.misses).containsExactlyInAnyOrder("a", "o", "g");
159+
assertThat(first.status).isEqualTo(Status.WIN);
159160

160161
Output second = results.get(1);
161-
assertEquals("abba", second.discovered);
162-
assertEquals(Set.of("a", "b"), second.guess);
163-
assertEquals(Set.of("e", "s"), second.misses);
164-
assertEquals(Status.WIN, first.status);
162+
assertThat(second.discovered).isEqualTo("abba");
163+
assertThat(second.guess).containsExactlyInAnyOrder("a", "b");
164+
assertThat(second.misses).containsExactlyInAnyOrder("e", "s");
165+
assertThat(first.status).isEqualTo(Status.WIN);
165166
} finally {
166167
subscription.dispose();
167168
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import org.junit.Test;
22

3-
import static org.junit.Assert.assertEquals;
3+
import static org.assertj.core.api.Assertions.assertThat;
44

55
public class GreeterTest {
66

77
@Test
88
public void testThatGreeterReturnsTheCorrectGreeting() {
9-
assertEquals("Hello, World!", new Greeter().getGreeting());
9+
assertThat(new Greeter().getGreeting()).isEqualTo("Hello, World!");
1010
}
1111

1212
}
Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,66 @@
11
import org.junit.Test;
22
import org.junit.Ignore;
3-
import static org.junit.Assert.assertEquals;
43

5-
public class HexadecimalTest {
4+
import static org.assertj.core.api.Assertions.assertThat;
65

6+
public class HexadecimalTest {
77

88
@Test
99
public void testOne() {
10-
int expected = 1;
11-
assertEquals(expected, Hexadecimal.toDecimal("1"));
10+
assertThat(Hexadecimal.toDecimal("1")).isEqualTo(1);
1211
}
1312

1413
@Ignore
1514
@Test
1615
public void testC() {
17-
int expected = 12;
18-
assertEquals(expected, Hexadecimal.toDecimal("c"));
16+
assertThat(Hexadecimal.toDecimal("c")).isEqualTo(12);
1917
}
2018

2119
@Ignore
2220
@Test
2321
public void test10() {
24-
int expected = 16;
25-
assertEquals(expected, Hexadecimal.toDecimal("10"));
22+
assertThat(Hexadecimal.toDecimal("10")).isEqualTo(16);
2623
}
2724

2825
@Ignore
2926
@Test
3027
public void testAf() {
31-
int expected = 175;
32-
assertEquals(expected, Hexadecimal.toDecimal("af"));
28+
assertThat(Hexadecimal.toDecimal("af")).isEqualTo(175);
3329
}
3430

3531
@Ignore
3632
@Test
3733
public void test100() {
38-
int expected = 256;
39-
assertEquals(expected, Hexadecimal.toDecimal("100"));
34+
assertThat(Hexadecimal.toDecimal("100")).isEqualTo(256);
4035
}
4136

4237
@Ignore
4338
@Test
4439
public void test19ace() {
45-
int expected = 105166;
46-
assertEquals(expected, Hexadecimal.toDecimal("19ace"));
40+
assertThat(Hexadecimal.toDecimal("19ace")).isEqualTo(105166);
4741
}
4842

4943
@Ignore
5044
@Test
5145
public void testInvalid() {
52-
int expected = 0;
53-
assertEquals(expected, Hexadecimal.toDecimal("carrot"));
46+
assertThat(Hexadecimal.toDecimal("carrot")).isEqualTo(0);
5447
}
5548

5649
@Ignore
5750
@Test
5851
public void testBlack() {
59-
int expected = 0;
60-
assertEquals(expected, Hexadecimal.toDecimal("000000"));
52+
assertThat(Hexadecimal.toDecimal("000000")).isEqualTo(0);
6153
}
6254

6355
@Ignore
6456
@Test
6557
public void testWhite() {
66-
int expected = 16777215;
67-
assertEquals(expected, Hexadecimal.toDecimal("ffffff"));
58+
assertThat(Hexadecimal.toDecimal("ffffff")).isEqualTo(16777215);
6859
}
6960

7061
@Ignore
7162
@Test
7263
public void testYellow() {
73-
int expected = 16776960;
74-
assertEquals(expected, Hexadecimal.toDecimal("ffff00"));
64+
assertThat(Hexadecimal.toDecimal("ffff00")).isEqualTo(16776960);
7565
}
7666
}

0 commit comments

Comments
 (0)