From 2f34022dedcdd4c480ed228807ced76b6eed7ff9 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Tue, 31 Oct 2023 18:11:34 +0530 Subject: [PATCH] Test Case Update --- .../FirstMatchingElementUnitTest.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java index 373833e9f7..48171bab65 100644 --- a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java +++ b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java @@ -3,6 +3,7 @@ package com.baeldung.streams.firstmatchingelement; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.IntStream; import org.apache.commons.collections4.IterableUtils; @@ -17,12 +18,17 @@ public class FirstMatchingElementUnitTest { private String searchName = "John"; @Test - public void whenUsingIndexOf_thenFindFirstMatchingUserIndex() { + public void whenUsingStream_thenFindFirstMatchingUserIndex() { + AtomicInteger counter = new AtomicInteger(-1); int index = userList.stream() - .filter(user -> searchName.equals(user.getUserName())) - .mapToInt(user -> userList.indexOf(user)) + .filter(user -> { + counter.getAndIncrement(); + return searchName.equals(user.getUserName()); + }) + .map(user -> counter.get()) .findFirst() .orElse(-1); + assertEquals(1, index); }