Skip to content

Commit 306eb4e

Browse files
author
Tomasz Lelek
committed
BAEL-602 example of using Unchecked
1 parent 8d4b696 commit 306eb4e

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

‎jooq/src/test/java/com/baeldung/JOOLTest.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33

44
import org.jooq.lambda.Seq;
5+
import org.jooq.lambda.Unchecked;
56
import org.junit.Test;
67

78
import java.util.Arrays;
89
import java.util.HashMap;
910
import java.util.List;
1011
import java.util.Map;
12+
import java.util.stream.Collectors;
1113

1214
import static junit.framework.Assert.assertTrue;
1315
import static junit.framework.TestCase.assertEquals;
@@ -148,4 +150,44 @@ public void givenSeq_whenZip_shouldHaveZippedSeq() {
148150
Arrays.asList(tuple("a", 0L), tuple("b", 1L), tuple("c", 2L))
149151
);
150152
}
153+
154+
155+
public Integer methodThatThrowsChecked(String arg) throws Exception {
156+
return arg.length();
157+
}
158+
159+
@Test
160+
public void givenOperationThatThrowsCheckedException_whenExecuteAndNeedToWrapCheckedIntoUnchecked_shouldPass() {
161+
//when
162+
List<Integer> collect = Arrays.asList("a", "b", "c").stream().map(elem -> {
163+
try {
164+
return methodThatThrowsChecked(elem);
165+
} catch (Exception e) {
166+
e.printStackTrace();
167+
throw new RuntimeException(e);
168+
}
169+
}).collect(Collectors.toList());
170+
171+
//then
172+
assertEquals(
173+
collect,
174+
Arrays.asList(1, 1, 1)
175+
);
176+
}
177+
178+
179+
@Test
180+
public void givenOperationThatThrowsCheckedException_whenExecuteUsingUncheckedFuction_shouldPass() {
181+
//when
182+
List<Integer> collect = Arrays.asList("a", "b", "c").stream()
183+
.map(Unchecked.function(elem -> methodThatThrowsChecked(elem)))
184+
.collect(Collectors.toList());
185+
186+
//then
187+
assertEquals(
188+
collect,
189+
Arrays.asList(1, 1, 1)
190+
);
191+
}
192+
151193
}

0 commit comments

Comments
 (0)