Skip to Content
Functional Programming in Java
book

Functional Programming in Java

by Venkat Subramaniam
February 2014
Intermediate to advanced
160 pages
4h 59m
English
Pragmatic Bookshelf
Content preview from Functional Programming in Java

Creating Concise Exception Tests

When Java 5 annotations were introduced, JUnit was quick to use them. Overall this has been a benefit, but one use in particular, the convenience of exception tests, leads to terse rather than concise code. Let’s understand the issues and then resolve them using—good guess—lambda expressions. We will see here that lambda expressions are not just another language feature; they alter the way we think and design applications.

Suppose we’re driving the design of a class, RodCutter, through unit tests and we expect a method maxProfit to throw an exception if the argument is zero. Let’s look at a few ways we can write the exception tests for it.

Exception Test with try and catch

Here’s the test for the maxProfit method ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Programming in Java

Functional Programming in Java

Pierre-Yves Saumont

Publisher Resources

ISBN: 9781941222690Errata Page