Last active
January 2, 2016 10:09
-
-
Save mariofusco/8287989 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.*; | |
import static junit.framework.Assert.assertEquals; | |
import static org.javaz.Try.attempt; | |
public class TryTest { | |
public String first(boolean throwEx) throws Exception { | |
if (throwEx) throw new RuntimeException("Cannot read first"); | |
return "6"; | |
} | |
public String second(boolean throwEx) throws Exception { | |
if (throwEx) throw new RuntimeException("Cannot read second"); | |
return "2"; | |
} | |
@Test | |
public void test() { | |
Try<Integer> result = attempt(() -> first(false)) | |
.map(Integer::parseInt) | |
.flatMap(first -> attempt(() -> second(false)).map(Integer::parseInt) | |
.map(second -> first / second)); | |
assertEquals(3, (int)result.orElse(-1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment