Functional Programming in Java, Second Edition: Code reorganization into JUnit tests rather than individual main-adorned classes

This may be too extensive to change, but I would like to see the code example not as a series of classes with main()
but as JUnit
test classes.
One can then have all the code for one chapter in single class, and execute the individual methods, each corresponding to an example, directly from the IDE, without messing around with calling this or that main()
.
I suppose everyone knowledgeable of Java knows about JUnit at this point.
For example, for a part of chapter 3:
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class MyTest {
private final static String theDir = "foo";
// "Listing Select Files in a Directory", p.61
@Test
public void listSelectFilesTheHardWay() {
final String[] files =
new File(theDir).list(new java.io.FilenameFilter() {
public boolean accept(final File _dir, final String name) {
return name.endsWith(".java");
}
});
String res =
(files == null) ?
("Looks like '" + theDir + "' is not a directory") :
(Arrays.stream(files).collect(Collectors.joining("\n")));
System.out.println(res);
}
// "Listing Select Files in a Directory", p.61
@Test
public void listSelectFilesTheGoodWay() {
try {
Files.newDirectoryStream(
Paths.get(theDir), path -> path.toString().endsWith(".java"))
.forEach(System.out::println);
} catch (IOException ex) {
System.out.println("Looks like '" + theDir + "' is not a directory, or something");
}
}
}
If one loads this into the IDE, one just needs to hit the green arrows next to the @Test
annotations for great results.
P.S.
I just noticed that an actual main()
makes an appearance on p.69, like a blast from the past. It really would look better as a test case.
Popular Prag Prog topics

Whilst the author has been careful to provide exact results for the tests elsewhere in the book (such as surds with the transformation te...
New

The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New

Hi Jamis,
I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019.
This test doesn’t pass for me:
...
New

Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this b...
New

This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New

In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions:
In Gemfile, change
gem 'rails...
New

I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New

Is the book’s epub format available to read on Google Play Books?
New

Modern Front-End Development for Rails - application does not start after run bin/setup (page xviii)
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New

@parrt
In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
Other popular topics

I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New

Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face:
Perhaps if there’s enough peop...
New
New

Do the test and post your score :nerd_face:
:keyboard:
If possible, please add info such as the keyboard you’re using, the layout (Qw...
New

Small essay with thoughts on macOS vs. Linux:
I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New

The V Programming Language
Simple language for building maintainable programs
V is already mentioned couple of times in the forum, but I...
New

Saw this on TikTok of all places! :lol:
Anyone heard of them before?
Lite:
New

This is going to be a long an frequently posted thread.
While talking to a friend of mine who has taken data structure and algorithm cou...
New

The overengineered Solution to my Pigeon Problem.
TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New

Large Language Models like ChatGPT say The Darnedest Things.
The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
Latest in PragProg
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /preact
- /flutter
- /c-plus-plus
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm