From 498230ff89e752ea01a6fd71c081d8ab736fd3c1 Mon Sep 17 00:00:00 2001 From: pivovarit Date: Wed, 15 Feb 2017 20:48:46 +0100 Subject: [PATCH] Refactor MinMaxTest --- .../com/baeldung/java8/Java8MaxMinTest.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/java8/Java8MaxMinTest.java b/core-java/src/test/java/com/baeldung/java8/Java8MaxMinTest.java index 0e361d96e1..b0e514124d 100644 --- a/core-java/src/test/java/com/baeldung/java8/Java8MaxMinTest.java +++ b/core-java/src/test/java/com/baeldung/java8/Java8MaxMinTest.java @@ -4,7 +4,9 @@ import com.baeldung.java_8_features.Person; import org.junit.Test; import java.util.Arrays; +import java.util.Comparator; import java.util.List; +import java.util.NoSuchElementException; import static org.junit.Assert.assertEquals; @@ -17,8 +19,12 @@ public class Java8MaxMinTest { final Integer expectedResult = 89; //then - assertEquals(expectedResult, - (Integer) listOfIntegers.stream().mapToInt(val -> val).max().getAsInt()); + final Integer max = listOfIntegers + .stream() + .mapToInt(v -> v) + .max().orElseThrow(NoSuchElementException::new); + + assertEquals("Should be 89", expectedResult, max); } @Test @@ -30,8 +36,12 @@ public class Java8MaxMinTest { final List people = Arrays.asList(alex, john, peter); //then - assertEquals("Alex must be having min age", alex, - people.stream().min((o1, o2) -> o1.getAge().compareTo(o2.getAge())).get()); + final Person minByAge = people + .stream() + .min(Comparator.comparing(Person::getAge)) + .orElseThrow(NoSuchElementException::new); + + assertEquals("Should be Alex", alex, minByAge); } }