Skip to content

Commit 05b2802

Browse files
committed
Test for another task.
1 parent af7a65d commit 05b2802

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

src/test/java/de/codecentric/java8examples/streaming/CollectingAndReducingTest.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package de.codecentric.java8examples.streaming;
22

33
import de.codecentric.java8examples.Invoice;
4+
import de.codecentric.java8examples.InvoiceItem;
45
import de.codecentric.java8examples.Person;
56
import de.codecentric.java8examples.TestData;
67
import org.junit.Test;
78

89
import java.math.BigDecimal;
9-
import java.util.Arrays;
10-
import java.util.IntSummaryStatistics;
11-
import java.util.List;
12-
import java.util.Map;
10+
import java.util.*;
1311

1412
import static junit.framework.Assert.assertNotNull;
1513
import static org.hamcrest.Matchers.*;
@@ -109,7 +107,24 @@ public void testExpensesByRecipient() throws Exception {
109107

110108
@Test
111109
public void testCountByProduct() throws Exception {
110+
Map<String, Integer> expected = new HashMap<>();
111+
for (Invoice invoice: invoices) {
112+
for (InvoiceItem item: invoice.getItems()) {
113+
String product = item.getProduct();
114+
if (expected.get(product) == null) {
115+
expected.put(product, Integer.valueOf(0));
116+
}
117+
expected.put(
118+
product,
119+
expected.get(product) + item.getQuantity());
120+
}
121+
}
112122

123+
Map<String, Integer> actual = CollectingAndReducing.countByProduct(invoices);
124+
assertThat(actual.keySet(), hasSize(expected.size()));
125+
for (Map.Entry entry: expected.entrySet()) {
126+
assertThat(actual, hasEntry(entry.getKey(), entry.getValue()));
127+
}
113128
}
114129

115130
@Test

0 commit comments

Comments
 (0)