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

**URL:** <https://forum.devtalk.com/t/functional-programming-in-java-second-edition-code-reorganization-into-junit-tests-rather-than-individual-main-adorned-classes/105748>\
**Category:** PragProg Customers\
**Tags:** book-functional-programming-in-java-second-edition, suggestion\
**Created:** [20 April 2023 13:43 UTC](https://forum.devtalk.com/t/functional-programming-in-java-second-edition-code-reorganization-into-junit-tests-rather-than-individual-main-adorned-classes/105748 "2023-04-20T13:43:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtonhofer](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dtonhofer/32/3389_2.png) [@dtonhofer](https://forum.devtalk.com/u/dtonhofer)\
**Post date:** [20 April 2023 13:43 UTC](https://forum.devtalk.com/t/functional-programming-in-java-second-edition-code-reorganization-into-junit-tests-rather-than-individual-main-adorned-classes/105748/1 "2023-04-20T13:43:49Z")

</div>

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:

```auto
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.
