BAEL-580 handling exceptions in lambda expressions (#1004)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
deb694bc88
commit
0aa46996a6
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.java8.lambda.exceptions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.baeldung.java8.lambda.exceptions.LambdaExceptionWrappers.*;
|
||||
|
||||
public class LambdaExceptionWrappersTest {
|
||||
|
||||
private List<Integer> integers;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
integers = Arrays.asList(3, 9, 7, 0, 10, 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoExceptionFromLambdaWrapper_thenSuccess() {
|
||||
integers.forEach(lambdaWrapper(i -> System.out.println(50 / i)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoExceptionFromConsumerWrapper_thenSuccess() {
|
||||
integers.forEach(consumerWrapper(i -> System.out.println(50 / i), ArithmeticException.class));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void whenExceptionFromThrowingConsumerWrapper_thenSuccess() {
|
||||
integers.forEach(throwingConsumerWrapper(i -> writeToFile(i)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoExceptionFromHandlingConsumerWrapper_thenSuccess() {
|
||||
integers.forEach(handlingConsumerWrapper(i -> writeToFile(i), IOException.class));
|
||||
}
|
||||
|
||||
void writeToFile(Integer i) throws IOException {
|
||||
if (i == 0) {
|
||||
throw new IOException(); // mock IOException
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user