|
| 1 | +package com.baeldung.findastring; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | +import org.junit.Test; |
| 7 | +public class FindAStringInListUnitTest { |
| 8 | + |
| 9 | + private static List<String> list = new ArrayList<>(); |
| 10 | + |
| 11 | + static { |
| 12 | + list.add("Jack and Jill"); |
| 13 | + list.add("James and Sarah"); |
| 14 | + list.add("Sam and Louise"); |
| 15 | + list.add("Jack"); |
| 16 | + list.add(""); |
| 17 | + } |
| 18 | + |
| 19 | + private static FindAStringInGivenList findAStringInGivenList = new FindAStringInGivenList(); |
| 20 | + |
| 21 | + @Test |
| 22 | + public void givenAString_whenFoundUsingLoopWithRegex_thenReturnList() { |
| 23 | + List matchingElements = findAStringInGivenList.findUsingLoopWithRegex("Jack", list); |
| 24 | + assertEquals(2, matchingElements.size()); |
| 25 | + assertEquals("Jack and Jill", matchingElements.get(0)); |
| 26 | + assertEquals("Jack", matchingElements.get(1)); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenAString_whenFoundUsingLoop_thenReturnList() { |
| 31 | + List matchingElements = findAStringInGivenList.findUsingLoop("Jack", list); |
| 32 | + assertEquals(2, matchingElements.size()); |
| 33 | + assertEquals("Jack and Jill", matchingElements.get(0)); |
| 34 | + assertEquals("Jack", matchingElements.get(1)); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + @Test |
| 39 | + public void givenAString_whenFoundUsingStream_thenReturnList(){ |
| 40 | + List matchingElements = findAStringInGivenList.findUsingStream("Jack", list); |
| 41 | + assertEquals(2, matchingElements.size()); |
| 42 | + assertEquals("Jack and Jill", matchingElements.get(0)); |
| 43 | + assertEquals("Jack", matchingElements.get(1)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void givenAString_whenFoundUsingCommonsCollection_thenReturnList(){ |
| 48 | + List matchingElements = findAStringInGivenList.findUsingCommonsCollection("Jack", list); |
| 49 | + assertEquals(2, matchingElements.size()); |
| 50 | + assertEquals("Jack and Jill", matchingElements.get(0)); |
| 51 | + assertEquals("Jack", matchingElements.get(1)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenAString_whenFoundUsingGuava_thenReturnList(){ |
| 56 | + List matchingElements = findAStringInGivenList.findUsingGuava("Jack", list); |
| 57 | + assertEquals(2, matchingElements.size()); |
| 58 | + assertEquals("Jack and Jill", matchingElements.get(0)); |
| 59 | + assertEquals("Jack", matchingElements.get(1)); |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments