All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.rules.ExpectedExceptionMatcherBuilder Maven / Gradle / Ivy

Go to download

JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.

There is a newer version: 4.13.2
Show newest version
package org.junit.rules;

import static org.hamcrest.CoreMatchers.allOf;
import static org.junit.matchers.JUnitMatchers.isThrowable;

import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Matcher;

/**
 * Builds special matcher used by {@link ExpectedException}.
 */
class ExpectedExceptionMatcherBuilder {

    private final List> matchers = new ArrayList>();

    void add(Matcher matcher) {
        matchers.add(matcher);
    }

    boolean expectsThrowable() {
        return !matchers.isEmpty();
    }

    Matcher build() {
        return isThrowable(allOfTheMatchers());
    }

    private Matcher allOfTheMatchers() {
        if (matchers.size() == 1) {
            return cast(matchers.get(0));
        }
        return allOf(castedMatchers());
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    private List> castedMatchers() {
        return new ArrayList>((List) matchers);
    }

    @SuppressWarnings("unchecked")
    private Matcher cast(Matcher singleMatcher) {
        return (Matcher) singleMatcher;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy