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

belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
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
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

Other popular topics Top

siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
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
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: