Skip to content

Commit 997e63b

Browse files
authored
Converting tests to AssertJ for exercises starting with B (exercism#2116)
1 parent b7c0820 commit 997e63b

7 files changed

Lines changed: 341 additions & 379 deletions

File tree

exercises/practice/bank-account/src/test/java/BankAccountTest.java

Lines changed: 6 additions & 7 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
import static org.junit.Assert.fail;
54

@@ -15,7 +14,7 @@ public class BankAccountTest {
1514
public void newlyOpenedAccountHasEmptyBalance() throws BankAccountActionInvalidException {
1615
bankAccount.open();
1716

18-
assertEquals(0, bankAccount.getBalance());
17+
assertThat(bankAccount.getBalance()).isEqualTo(0);
1918
}
2019

2120
@Ignore("Remove to run test")
@@ -25,7 +24,7 @@ public void canDepositMoney() throws BankAccountActionInvalidException {
2524

2625
bankAccount.deposit(10);
2726

28-
assertEquals(10, bankAccount.getBalance());
27+
assertThat(bankAccount.getBalance()).isEqualTo(10);
2928
}
3029

3130
@Ignore("Remove to run test")
@@ -36,7 +35,7 @@ public void canDepositMoneySequentially() throws BankAccountActionInvalidExcepti
3635
bankAccount.deposit(5);
3736
bankAccount.deposit(23);
3837

39-
assertEquals(28, bankAccount.getBalance());
38+
assertThat(bankAccount.getBalance()).isEqualTo(28);
4039
}
4140

4241
@Ignore("Remove to run test")
@@ -47,7 +46,7 @@ public void canWithdrawMoney() throws BankAccountActionInvalidException {
4746

4847
bankAccount.withdraw(5);
4948

50-
assertEquals(5, bankAccount.getBalance());
49+
assertThat(bankAccount.getBalance()).isEqualTo(5);
5150
}
5251

5352
@Ignore("Remove to run test")
@@ -59,7 +58,7 @@ public void canWithdrawMoneySequentially() throws BankAccountActionInvalidExcept
5958
bankAccount.withdraw(10);
6059
bankAccount.withdraw(13);
6160

62-
assertEquals(0, bankAccount.getBalance());
61+
assertThat(bankAccount.getBalance()).isEqualTo(0);
6362
}
6463

6564
@Ignore("Remove to run test")
@@ -185,7 +184,7 @@ public void canAdjustBalanceConcurrently() throws BankAccountActionInvalidExcept
185184

186185
for (int i = 0; i < 10; i++) {
187186
adjustBalanceConcurrently();
188-
assertEquals(1000, bankAccount.getBalance());
187+
assertThat(bankAccount.getBalance()).isEqualTo(1000);
189188
}
190189
}
191190

exercises/practice/beer-song/src/test/java/BeerSongTest.java

Lines changed: 222 additions & 229 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11

2-
import java.util.Arrays;
32
import java.util.Collections;
43
import java.util.List;
5-
import static org.junit.Assert.assertEquals;
6-
import static org.junit.Assert.assertNotNull;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
76
import org.junit.Ignore;
87
import org.junit.Test;
98

@@ -17,31 +16,31 @@ public void dataIsRetained() {
1716
binarySearchTree.insert(expected);
1817

1918
BinarySearchTree.Node<Integer> root = binarySearchTree.getRoot();
20-
assertNotNull(root);
2119

22-
int actual = root.getData();
23-
assertEquals(expected, actual);
20+
assertThat(root).isNotNull();
21+
assertThat(root.getData()).isEqualTo(expected);
2422
}
2523

24+
2625
@Ignore("Remove to run test")
2726
@Test
2827
public void insertsLess() {
2928
BinarySearchTree<Character> binarySearchTree = new BinarySearchTree<>();
29+
3030
char expectedRoot = '4';
3131
char expectedLeft = '2';
3232

3333
binarySearchTree.insert(expectedRoot);
3434
binarySearchTree.insert(expectedLeft);
3535

3636
BinarySearchTree.Node<Character> root = binarySearchTree.getRoot();
37-
assertNotNull(root);
3837
BinarySearchTree.Node<Character> left = root.getLeft();
39-
assertNotNull(left);
4038

41-
char actualRoot = root.getData();
42-
char actualLeft = left.getData();
43-
assertEquals(expectedLeft, actualLeft);
44-
assertEquals(expectedRoot, actualRoot);
39+
assertThat(root).isNotNull();
40+
assertThat(left).isNotNull();
41+
42+
assertThat(root.getData()).isEqualTo(expectedRoot);
43+
assertThat(left.getData()).isEqualTo(expectedLeft);
4544
}
4645

4746
@Ignore("Remove to run test")
@@ -55,14 +54,13 @@ public void insertsSame() {
5554
binarySearchTree.insert(expectedLeft);
5655

5756
BinarySearchTree.Node<String> root = binarySearchTree.getRoot();
58-
assertNotNull(root);
5957
BinarySearchTree.Node<String> left = root.getLeft();
60-
assertNotNull(left);
6158

62-
String actualRoot = root.getData();
63-
String actualLeft = left.getData();
64-
assertEquals(expectedLeft, actualLeft);
65-
assertEquals(expectedRoot, actualRoot);
59+
assertThat(root).isNotNull();
60+
assertThat(left).isNotNull();
61+
62+
assertThat(root.getData()).isEqualTo(expectedRoot);
63+
assertThat(left.getData()).isEqualTo(expectedLeft);
6664
}
6765

6866
@Ignore("Remove to run test")
@@ -76,106 +74,83 @@ public void insertsRight() {
7674
binarySearchTree.insert(expectedRight);
7775

7876
BinarySearchTree.Node<Integer> root = binarySearchTree.getRoot();
79-
assertNotNull(root);
8077
BinarySearchTree.Node<Integer> right = root.getRight();
81-
assertNotNull(right);
8278

83-
int actualRoot = root.getData();
84-
int actualRight = right.getData();
85-
assertEquals(expectedRight, actualRight);
86-
assertEquals(expectedRoot, actualRoot);
79+
assertThat(root).isNotNull();
80+
assertThat(right).isNotNull();
81+
82+
assertThat(root.getData()).isEqualTo(expectedRoot);
83+
assertThat(right.getData()).isEqualTo(expectedRight);
8784
}
8885

8986
@Ignore("Remove to run test")
9087
@Test
9188
public void createsComplexTree() {
9289
BinarySearchTree<Character> binarySearchTree = new BinarySearchTree<>();
93-
List<Character> expected = Collections.unmodifiableList(
94-
Arrays.asList('4', '2', '6', '1', '3', '5', '7')
95-
);
90+
List<Character> expected = List.of('4', '2', '6', '1', '3', '5', '7');
9691

97-
List<Character> treeData = Collections.unmodifiableList(
98-
Arrays.asList('4', '2', '6', '1', '3', '5', '7')
99-
);
92+
List<Character> treeData = List.of('4', '2', '6', '1', '3', '5', '7');
10093
treeData.forEach(binarySearchTree::insert);
10194

102-
List<Character> actual = binarySearchTree.getAsLevelOrderList();
103-
assertEquals(expected, actual);
95+
assertThat(binarySearchTree.getAsLevelOrderList()).isEqualTo(expected);
10496
}
10597

10698
@Ignore("Remove to run test")
10799
@Test
108100
public void sortsSingleElement() {
109101
BinarySearchTree<String> binarySearchTree = new BinarySearchTree<>();
110-
List<String> expected = Collections.unmodifiableList(
111-
Collections.singletonList("2")
112-
);
102+
List<String> expected = Collections.singletonList("2");
113103

114104
binarySearchTree.insert("2");
115105

116-
List<String> actual = binarySearchTree.getAsSortedList();
117-
assertEquals(expected, actual);
106+
assertThat(binarySearchTree.getAsSortedList()).isEqualTo(expected);
118107
}
119108

120109
@Ignore("Remove to run test")
121110
@Test
122111
public void sortsCollectionOfTwoIfSecondInsertedIsSmallerThanFirst() {
123112
BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();
124-
List<Integer> expected = Collections.unmodifiableList(
125-
Arrays.asList(1, 2)
126-
);
113+
List<Integer> expected = List.of(1, 2);
127114

128115
binarySearchTree.insert(2);
129116
binarySearchTree.insert(1);
130117

131-
List<Integer> actual = binarySearchTree.getAsSortedList();
132-
assertEquals(expected, actual);
118+
assertThat(binarySearchTree.getAsSortedList()).isEqualTo(expected);
133119
}
134120

135121
@Ignore("Remove to run test")
136122
@Test
137123
public void sortsCollectionOfTwoIfSecondNumberisSameAsFirst() {
138124
BinarySearchTree<Character> binarySearchTree = new BinarySearchTree<>();
139-
List<Character> expected = Collections.unmodifiableList(
140-
Arrays.asList('2', '2')
141-
);
125+
List<Character> expected = List.of('2', '2');
142126

143127
binarySearchTree.insert('2');
144128
binarySearchTree.insert('2');
145129

146-
List<Character> actual = binarySearchTree.getAsSortedList();
147-
assertEquals(expected, actual);
130+
assertThat(binarySearchTree.getAsSortedList()).isEqualTo(expected);
148131
}
149132

150133
@Ignore("Remove to run test")
151134
@Test
152135
public void sortsCollectionOfTwoIfSecondInsertedIsBiggerThanFirst() {
153136
BinarySearchTree<Character> binarySearchTree = new BinarySearchTree<>();
154-
List<Character> expected = Collections.unmodifiableList(
155-
Arrays.asList('2', '3')
156-
);
137+
List<Character> expected = List.of('2', '3');
157138

158139
binarySearchTree.insert('2');
159140
binarySearchTree.insert('3');
160141

161-
List<Character> actual = binarySearchTree.getAsSortedList();
162-
assertEquals(expected, actual);
142+
assertThat(binarySearchTree.getAsSortedList()).isEqualTo(expected);
163143
}
164144

165145
@Ignore("Remove to run test")
166146
@Test
167147
public void iteratesOverComplexTree() {
168148
BinarySearchTree<String> binarySearchTree = new BinarySearchTree<>();
169-
List<String> expected = Collections.unmodifiableList(
170-
Arrays.asList("1", "2", "3", "5", "6", "7")
171-
);
149+
List<String> expected = List.of("1", "2", "3", "5", "6", "7");
172150

173-
List<String> treeData = Collections.unmodifiableList(
174-
Arrays.asList("2", "1", "3", "6", "7", "5")
175-
);
151+
List<String> treeData = List.of("2", "1", "3", "6", "7", "5");
176152
treeData.forEach(binarySearchTree::insert);
177153

178-
List<String> actual = binarySearchTree.getAsSortedList();
179-
assertEquals(expected, actual);
154+
assertThat(binarySearchTree.getAsSortedList()).isEqualTo(expected);
180155
}
181156
}

exercises/practice/binary-search/src/test/java/BinarySearchTest.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
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;
65
import org.junit.Test;
76

8-
import java.util.Arrays;
97
import java.util.Collections;
108
import java.util.List;
119

@@ -17,75 +15,63 @@ public void findsAValueInAnArrayWithOneElement() throws ValueNotFoundException {
1715

1816
BinarySearch search = new BinarySearch(listOfUnitLength);
1917

20-
assertEquals(0, search.indexOf(6));
18+
assertThat(search.indexOf(6)).isEqualTo(0);
2119
}
2220

2321
@Ignore("Remove to run test")
2422
@Test
2523
public void findsAValueInTheMiddleOfAnArray() throws ValueNotFoundException {
26-
List<Integer> sortedList = Collections.unmodifiableList(
27-
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
28-
);
24+
List<Integer> sortedList = List.of(1, 3, 4, 6, 8, 9, 11);
2925

3026
BinarySearch search = new BinarySearch(sortedList);
3127

32-
assertEquals(3, search.indexOf(6));
28+
assertThat(search.indexOf(6)).isEqualTo(3);
3329
}
3430

3531
@Ignore("Remove to run test")
3632
@Test
3733
public void findsAValueAtTheBeginningOfAnArray() throws ValueNotFoundException {
38-
List<Integer> sortedList = Collections.unmodifiableList(
39-
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
40-
);
34+
List<Integer> sortedList = List.of(1, 3, 4, 6, 8, 9, 11);
4135

4236
BinarySearch search = new BinarySearch(sortedList);
4337

44-
assertEquals(0, search.indexOf(1));
38+
assertThat(search.indexOf(1)).isEqualTo(0);
4539
}
4640

4741
@Ignore("Remove to run test")
4842
@Test
4943
public void findsAValueAtTheEndOfAnArray() throws ValueNotFoundException {
50-
List<Integer> sortedList = Collections.unmodifiableList(
51-
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
52-
);
44+
List<Integer> sortedList = List.of(1, 3, 4, 6, 8, 9, 11);
5345

5446
BinarySearch search = new BinarySearch(sortedList);
5547

56-
assertEquals(6, search.indexOf(11));
48+
assertThat(search.indexOf(11)).isEqualTo(6);
5749
}
5850

5951
@Ignore("Remove to run test")
6052
@Test
6153
public void findsAValueInAnArrayOfOddLength() throws ValueNotFoundException {
62-
List<Integer> sortedListOfOddLength = Collections.unmodifiableList(
63-
Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634)
64-
);
54+
List<Integer> sortedListOfOddLength = List.of(1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634);
6555

6656
BinarySearch search = new BinarySearch(sortedListOfOddLength);
6757

68-
assertEquals(9, search.indexOf(144));
58+
assertThat(search.indexOf(144)).isEqualTo(9);
6959
}
7060

7161
@Ignore("Remove to run test")
7262
@Test
7363
public void findsAValueInAnArrayOfEvenLength() throws ValueNotFoundException {
74-
List<Integer> sortedListOfEvenLength = Collections.unmodifiableList(
75-
Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377)
76-
);
64+
List<Integer> sortedListOfEvenLength = List.of(1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377);
7765

7866
BinarySearch search = new BinarySearch(sortedListOfEvenLength);
7967

80-
assertEquals(5, search.indexOf(21));
68+
assertThat(search.indexOf(21)).isEqualTo(5);
8169
}
8270

8371
@Ignore("Remove to run test")
8472
@Test
8573
public void identifiesThatAValueIsNotFoundInTheArray() {
86-
List<Integer> sortedList = Collections.unmodifiableList(
87-
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
88-
);
74+
List<Integer> sortedList = List.of(1, 3, 4, 6, 8, 9, 11);
8975

9076
BinarySearch search = new BinarySearch(sortedList);
9177

@@ -100,9 +86,7 @@ public void identifiesThatAValueIsNotFoundInTheArray() {
10086
@Ignore("Remove to run test")
10187
@Test
10288
public void aValueSmallerThanTheArraysSmallestValueIsNotFound() {
103-
List<Integer> sortedList = Collections.unmodifiableList(
104-
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
105-
);
89+
List<Integer> sortedList = List.of(1, 3, 4, 6, 8, 9, 11);
10690

10791
BinarySearch search = new BinarySearch(sortedList);
10892

@@ -117,9 +101,7 @@ public void aValueSmallerThanTheArraysSmallestValueIsNotFound() {
117101
@Ignore("Remove to run test")
118102
@Test
119103
public void aValueLargerThanTheArraysSmallestValueIsNotFound() throws ValueNotFoundException {
120-
List<Integer> sortedList = Collections.unmodifiableList(
121-
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
122-
);
104+
List<Integer> sortedList = List.of(1, 3, 4, 6, 8, 9, 11);
123105

124106
BinarySearch search = new BinarySearch(sortedList);
125107

@@ -149,9 +131,7 @@ public void nothingIsFoundInAnEmptyArray() throws ValueNotFoundException {
149131
@Ignore("Remove to run test")
150132
@Test
151133
public void nothingIsFoundWhenTheLeftAndRightBoundCross() throws ValueNotFoundException {
152-
List<Integer> sortedList = Collections.unmodifiableList(
153-
Arrays.asList(1, 2)
154-
);
134+
List<Integer> sortedList = List.of(1, 2);
155135

156136
BinarySearch search = new BinarySearch(sortedList);
157137

0 commit comments

Comments
 (0)