Skip to content

Commit aaf5a3e

Browse files
gitterjim-Izhendrikse
authored andcommitted
Latest review changes: more concise code and suggested refactoring. (eugenp#1549)
* article Bael-667 initial commit. * Switch to use logging framework for output. * Make code more concise. Refactor as suggested.
1 parent 3c334e6 commit aaf5a3e

1 file changed

Lines changed: 8 additions & 38 deletions

File tree

‎core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java‎

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,39 @@
11
package com.baeldung.list.flattennestedlist;
22

3-
import static org.junit.Assert.assertEquals;
3+
import static java.util.Arrays.asList;
44
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertThat;
56
import static org.junit.Assert.assertTrue;
67

78
import java.util.ArrayList;
8-
import java.util.Arrays;
99
import java.util.Collection;
1010
import java.util.List;
1111
import java.util.stream.Collectors;
1212

13-
import org.junit.After;
14-
import org.junit.Before;
13+
import org.hamcrest.collection.IsIterableContainingInOrder;
1514
import org.junit.Test;
1615

1716
public class FlattenNestedListTest {
18-
private List<List<String>> lol = new ArrayList<>();
19-
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
20-
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
21-
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
22-
23-
@Before
24-
public void setup() {
25-
lol.addAll(Arrays.asList(ls1, ls2, ls3));
26-
}
27-
28-
@After
29-
public void tearDown() {
30-
lol = null;
31-
}
17+
List<List<String>> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four"));
3218

3319
@Test
3420
public void givenString_flattenNestedList1() {
3521
List<String> ls = flattenListOfListsImperatively(lol);
3622

3723
assertNotNull(ls);
38-
assertTrue(ls.size() == 9);
24+
assertTrue(ls.size() == 8);
3925
// assert content
40-
assertEquals(ls.get(0), "one:one");
41-
assertEquals(ls.get(1), "one:two");
42-
assertEquals(ls.get(2), "one:three");
43-
assertEquals(ls.get(3), "two:one");
44-
assertEquals(ls.get(4), "two:two");
45-
assertEquals(ls.get(5), "two:three");
46-
assertEquals(ls.get(6), "three:one");
47-
assertEquals(ls.get(7), "three:two");
48-
assertEquals(ls.get(8), "three:three");
26+
assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four"));
4927
}
5028

5129
@Test
5230
public void givenString_flattenNestedList2() {
5331
List<String> ls = flattenListOfListsStream(lol);
5432

5533
assertNotNull(ls);
56-
assertTrue(ls.size() == 9);
34+
assertTrue(ls.size() == 8);
5735
// assert content
58-
assertEquals(ls.get(0), "one:one");
59-
assertEquals(ls.get(1), "one:two");
60-
assertEquals(ls.get(2), "one:three");
61-
assertEquals(ls.get(3), "two:one");
62-
assertEquals(ls.get(4), "two:two");
63-
assertEquals(ls.get(5), "two:three");
64-
assertEquals(ls.get(6), "three:one");
65-
assertEquals(ls.get(7), "three:two");
66-
assertEquals(ls.get(8), "three:three");
36+
assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four"));
6737
}
6838

6939
public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {

0 commit comments

Comments
 (0)