Skip to content

Commit 498230f

Browse files
committed
Refactor MinMaxTest
1 parent 947fdda commit 498230f

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

‎core-java/src/test/java/com/baeldung/java8/Java8MaxMinTest.java‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.junit.Test;
55

66
import java.util.Arrays;
7+
import java.util.Comparator;
78
import java.util.List;
9+
import java.util.NoSuchElementException;
810

911
import static org.junit.Assert.assertEquals;
1012

@@ -17,8 +19,12 @@ public void whenListIsOfIntegerThenMaxCanBeDoneUsingIntegerComparator() {
1719
final Integer expectedResult = 89;
1820

1921
//then
20-
assertEquals(expectedResult,
21-
(Integer) listOfIntegers.stream().mapToInt(val -> val).max().getAsInt());
22+
final Integer max = listOfIntegers
23+
.stream()
24+
.mapToInt(v -> v)
25+
.max().orElseThrow(NoSuchElementException::new);
26+
27+
assertEquals("Should be 89", expectedResult, max);
2228
}
2329

2430
@Test
@@ -30,8 +36,12 @@ public void whenListIsOfPersonObjectThenMinCanBeDoneUsingCustomComparatorThrough
3036
final List<Person> people = Arrays.asList(alex, john, peter);
3137

3238
//then
33-
assertEquals("Alex must be having min age", alex,
34-
people.stream().min((o1, o2) -> o1.getAge().compareTo(o2.getAge())).get());
39+
final Person minByAge = people
40+
.stream()
41+
.min(Comparator.comparing(Person::getAge))
42+
.orElseThrow(NoSuchElementException::new);
43+
44+
assertEquals("Should be Alex", alex, minByAge);
3545
}
3646

3747
}

0 commit comments

Comments
 (0)