Skip to content

Commit 043a6bd

Browse files
varomorfpivovarit
authored andcommitted
Code for Optional stream filtering document (eugenp#696)
* Add new module for mocks comparison. * Add sources for testing. * Changes on testCase. * Enter some tests for mockito. * More tests for Mockito. * Even more tests. * Add the rest of the mocking libraries. * Javadoc on test. * Test bare bones for EasyMock. * Fist kind of test and setup. * Add tests using EasyMock with a change on LoginService. * Create LoginControllerTest.java * Test setup * [JMockit] No method called test. * [JMockit] Two methods called test. * [JMockit] One method called test. * [JMockit] Exception mock test * [JMockit] Mocked object to pass around test. * [JMockit] Custom matcher test. * [JMockit] Partial mocking test. * [JMockit] Fix with IDE. * Not stubs. Mocks. MOCKS!!! * Remove unnecesary import. * Use correct encoding. Was having problems with buildings. * Remove failing module. * Create new module mocks and move mock-comparisons there. * Add jmockit module. * Add model class. * Add collaborator class. * Add performer class. * Add performer test. * Fix * Add interface for tests. * Test for any. * Test for with. * Test for null. * Test for times. * Test for arg that. * Test for result and returns. * Test for delegate. * Add verifications to any tests. * Add verifications to with test. * Add verification examples to methods using null. * Add verifications to methods using times. * Formatting. * Compress tests and fix one test. * Adding new article to readme. * [BAEL-178] Add collaborator for advanced article. * [BAEL-178] Add link to readme. * [BAEL-178] Add test for mockUp. * [BAEL-178] Add test for invoke method. * [BAEL-178] Add constructors and tests for mockup for constructors. * [BAEL-178] Add private fields and more test for deencapsulation. * [BAEL-178] Add inner class and test for instantiating inner classes. * [BAEL-178] Multimocks. * [BAEL-178] Add test for expectation reusing. * [BAEL-178] Move test class to tests folders. * Add postgresql dependency. * Add test and config with properties. * [BAEL-114] Add new project for JPA with JNDI. * [BAEL-114] Config without xml. * [BAEL-114] Bring part of Foo, FooServie and FooDao. * [BAEL-114] Show all foos. * [BAEL-114] Readme. * [BAEL-114] Undo changes on main jpa project. * [BAEL-114] Remove unnecesary dependencies. * [BAEL-114] Add tomcat config. * [BAEL-114] Fixes. * Add tests for Optional streams. * Add Java 9 version of the test. * Rename and move to new core-java-9 module.
1 parent eaa04c6 commit 043a6bd

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.java8;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.Optional;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
public class Java9OptionalsStreamTest {
15+
16+
private List<Optional<String>> listOfOptionals;
17+
18+
@Before
19+
public void populateStream() {
20+
listOfOptionals = Arrays.asList(Optional.empty(), Optional.of("foo"), Optional.empty(), Optional.of("bar"));
21+
}
22+
23+
@Test
24+
public void filterOutPresentOptionalsWithFilter() {
25+
assertEquals(4, listOfOptionals.size());
26+
//@format:off
27+
List<String> filteredList = listOfOptionals.stream()
28+
.filter(Optional::isPresent)
29+
.map(Optional::get)
30+
.collect(Collectors.toList());
31+
//@format:on
32+
assertEquals(2, filteredList.size());
33+
assertEquals("foo", filteredList.get(0));
34+
assertEquals("bar", filteredList.get(1));
35+
}
36+
37+
@Test
38+
public void filterOutPresentOptionalsWithFlatMap() {
39+
assertEquals(4, listOfOptionals.size());
40+
//@format:off
41+
List<String> filteredList = listOfOptionals.stream()
42+
.flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty())
43+
.collect(Collectors.toList());
44+
//@format:on
45+
assertEquals(2, filteredList.size());
46+
assertEquals("foo", filteredList.get(0));
47+
assertEquals("bar", filteredList.get(1));
48+
}
49+
50+
@Test
51+
public void filterOutPresentOptionalsWithJava9() {
52+
assertEquals(4, listOfOptionals.size());
53+
//@format:off
54+
List<String> filteredList = listOfOptionals.stream()
55+
.flatMap(Optional::stream)
56+
.collect(Collectors.toList());
57+
//@format:on
58+
assertEquals(2, filteredList.size());
59+
assertEquals("foo", filteredList.get(0));
60+
assertEquals("bar", filteredList.get(1));
61+
}
62+
63+
}

0 commit comments

Comments
 (0)