Skip to content

Commit 379eab8

Browse files
committed
Implemented Stream forEach if/else unit test
1 parent cdee973 commit 379eab8

2 files changed

Lines changed: 41 additions & 42 deletions

File tree

‎core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java‎

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.stream.conditional;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Stream;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class StreamForEachIfElseUnitTest {
11+
12+
@Test
13+
public final void givenIntegerStream_whenCheckingIntegerParityWithIfElse_thenEnsureCorrectParity() {
14+
List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
15+
16+
ints.stream()
17+
.forEach(i -> {
18+
if (i.intValue() % 2 == 0) {
19+
Assert.assertTrue(i.intValue() + " is not even", i.intValue() % 2 == 0);
20+
} else {
21+
Assert.assertTrue(i.intValue() + " is not odd", i.intValue() % 2 != 0);
22+
}
23+
});
24+
25+
}
26+
27+
@Test
28+
public final void givenIntegerStream_whenCheckingIntegerParityWithStreamFilter_thenEnsureCorrectParity() {
29+
List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
30+
31+
Stream<Integer> evenIntegers = ints.stream()
32+
.filter(i -> i.intValue() % 2 == 0);
33+
Stream<Integer> oddIntegers = ints.stream()
34+
.filter(i -> i.intValue() % 2 != 0);
35+
36+
evenIntegers.forEach(i -> Assert.assertTrue(i.intValue() + " is not even", i.intValue() % 2 == 0));
37+
oddIntegers.forEach(i -> Assert.assertTrue(i.intValue() + " is not odd", i.intValue() % 2 != 0));
38+
39+
}
40+
41+
}

0 commit comments

Comments
 (0)