I have the following 2 logics and both produces same result tested against same test but the one that is implemented with a Streams produces code coverage issue with 80% and the one using for loop has no code coverage issue. Could I get some advice on why this is the case? Thanks.
This has no code coverage issues
public static Map<String, String> myMethod(List<KafkaHeader> headers) {
Map<String, String> map = new HashMap<>();
for(KafkaHeader header : headers) {
map.put(header.key(), String.valueOf(header.value()));
}
return map;
}
When using Stream as follows, there is coverage issue.
public static Map<String, String> myMethod(List<KafkaHeader> headers) {
return headers.stream()
.collect(Collectors.toMap(KafkaHeader::key, kafkaHeader -> String.valueOf(kafkaHeader.value())));
}
Test case:
@Test
public void simpleTest() {
List<KafkaHeader> headers = new ArrayList<>();
for (int i = 0; i < 10; i++) {
headers.add(new KafkaHeader("key"+i, "value"+i));
}
Map<String, String> headerMap = myclass.myMethod(headers);
for (int i = 0; i < 10; i++) {
assertEquals(headerMap.get("key"+i), "value"+i);
}
}
Note: KafkaHeader is an external class.

