dtonhofer

dtonhofer

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.

Where Next?

Popular Pragmatic Bookshelf topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
New
swlaschin
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
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1036 18873 386
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
hilfordjames
There appears to have been an update that has changed the terminology for what has previously been known as the Taskbar Overflow - this h...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: