DEV Community

Kasid Khan
Kasid Khan

Posted on

Why there are so many assertAll methods in Junit class AssertAll? What is the use of each.

class AssertAll {

    private AssertAll() {
        /* no-op */
    }

    static void assertAll(Executable... executables) {
        assertAll(null, executables);
    }

    static void assertAll(String heading, Executable... executables) {
        Preconditions.notEmpty(executables, "executables array must not be null or empty");
        Preconditions.containsNoNullElements(executables, "individual executables must not be null");
        assertAll(heading, Arrays.stream(executables));
    }

    static void assertAll(Collection<Executable> executables) {
        assertAll(null, executables);
    }

    static void assertAll(String heading, Collection<Executable> executables) {
        Preconditions.notNull(executables, "executables collection must not be null");
        Preconditions.containsNoNullElements(executables, "individual executables must not be null");
        assertAll(heading, executables.stream());
    }

    static void assertAll(Stream<Executable> executables) {
        assertAll(null, executables);
    }

    static void assertAll(String heading, Stream<Executable> executables) {
        Preconditions.notNull(executables, "executables stream must not be null");

        List<Throwable> failures = executables //
                .map(executable -> {
                    Preconditions.notNull(executable, "individual executables must not be null");
                    try {
                        executable.execute();
                        return null;
                    }
                    catch (Throwable t) {
                        UnrecoverableExceptions.rethrowIfUnrecoverable(t);
                        return t;
                    }
                }) //
                .filter(Objects::nonNull) //
                .collect(Collectors.toList());

        if (!failures.isEmpty()) {
            MultipleFailuresError multipleFailuresError = new MultipleFailuresError(heading, failures);
            failures.forEach(multipleFailuresError::addSuppressed);
            throw multipleFailuresError;
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
kasid_khan_98865d77a5fe2e profile image
Kasid Khan • Edited

Found the answer.
assertAll(Executable... executables) is calling assertAll(null,Executable... executables)
assertAll(null,Executable... executables) is calling assertAll(String heading, Stream executables), which is the actual method that does the execution.

assertAll(null,Executable... executables) == assertAll(heading,Executable... executables)
pheww !!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️