dtonhofer
Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 184 ff “Refactoring More Complex loops”
The same remarks apply as for “Refactoring the Traditional for Loop”
- No
init() - Classes under test implement a common interface that is called in tests.
- Negative tests which apply for input less than 1900, resulting in exceptions.
package chapter11; import org.junit.jupiter.api.Test; import java.time.Year; import java.util.stream.IntStream; import static org.junit.jupiter.api.Assertions.*; // Everything is inside the MoreComplexLoopTest class for convenience. // Documentation for java.time.Year.isLeap() // https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#isLeap-- public class MoreComplexLoopTest { interface LeapYears { int countFrom1900(int upTo); } static class LeapYearCountBefore implements LeapYears { public int countFrom1900(int upTo) { if (upTo < 1900) { throw new IllegalArgumentException("The passed value is < 1900: " + upTo); } int count = 0; for (int year = 1900; year <= upTo; year += 4) { if (Year.isLeap(year)) { count++; } } return count; } } static class LeapYearCountAfter implements LeapYears { public int countFrom1900(int upTo) { if (upTo < 1900) { throw new IllegalArgumentException("The passed value is < 1900: " + upTo); } return (int) IntStream.iterate(1900, year -> year <= upTo, year -> year + 4) .filter(Year::isLeap) .count(); } } private static void commonLeapYearsTests(final LeapYears leapYears) { assertAll( () -> assertEquals(0, leapYears.countFrom1900(1900)), () -> assertEquals(25, leapYears.countFrom1900(2000)), () -> assertEquals(27, leapYears.countFrom1900(2010)), () -> assertEquals(31, leapYears.countFrom1900(2025)), () -> assertEquals(49, leapYears.countFrom1900(2100)), () -> assertEquals(267, leapYears.countFrom1900(3000)), () -> assertThrows(IllegalArgumentException.class, () -> leapYears.countFrom1900(1800)) ); } @Test void leapYearCountBefore() { commonLeapYearsTests(new LeapYearCountBefore()); } @Test void leapYearCountAfter() { commonLeapYearsTests(new LeapYearCountAfter()); } } Popular Pragmatic Bookshelf topics
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New
Other popular topics
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
New
Tailwind CSS is an exciting new CSS framework that allows you to design your site by composing simple utility classes to create complex e...
New
Think Again 50% Off Sale » The theme of this sale is new perspectives on familiar topics. Enter coupon code ThinkAgain2021 at checkout t...
New
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
Explore the power of Ash Framework by modeling and building the domain for a real-world web application. Rebecca Le @sevenseacat and ...
New
Hair Salon Games for Girls Fun Girls Hair Saloon game is mainly developed for kids. This game allows users to select virtual avatars to ...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /html
- /opensuse
- /zig
- /centos
- /deepseek
- /php
- /scala
- /react-native
- /sublime-text
- /textmate
- /lisp
- /debian
- /nixos
- /agda
- /django
- /kubuntu
- /arch-linux
- /deno
- /nodejs
- /revery
- /ubuntu
- /spring
- /manjaro
- /diversity
- /lua
- /markdown
- /julia
- /slackware








