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

org.junit.experimental.results.ResultMatchers Maven / Gradle / Ivy

package org.junit.experimental.results;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.runner.notification.Failure;

/**
 * Matchers on a PrintableResult, to enable JUnit self-tests.
 * For example:
 *
 * 
 * assertThat(testResult(HasExpectedException.class), isSuccessful());
 * 
*/ public class ResultMatchers { /** * Do not instantiate. * @deprecated will be private soon. */ @Deprecated public ResultMatchers() { } /** * Matches if the tests are all successful */ public static Matcher isSuccessful() { return failureCountIs(0); } /** * Matches if there are {@code count} failures */ public static Matcher failureCountIs(final int count) { return new TypeSafeMatcher() { public void describeTo(Description description) { description.appendText("has " + count + " failures"); } @Override public boolean matchesSafely(PrintableResult item) { return item.failureCount() == count; } }; } /** * Matches if the result has exactly one failure, and it contains {@code string} */ public static Matcher hasSingleFailureContaining(final String string) { return new BaseMatcher() { public boolean matches(Object item) { return item.toString().contains(string) && failureCountIs(1).matches(item); } public void describeTo(Description description) { description.appendText("has single failure containing " + string); } }; } /** * Matches if the result has exactly one failure matching the given matcher. * * @since 4.13 */ public static Matcher hasSingleFailureMatching(final Matcher matcher) { return new TypeSafeMatcher() { @Override public boolean matchesSafely(PrintableResult item) { return item.failureCount() == 1 && matcher.matches(item.failures().get(0).getException()); } public void describeTo(Description description) { description.appendText("has failure with exception matching "); matcher.describeTo(description); } }; } /** * Matches if the result has one or more failures, and at least one of them * contains {@code string} */ public static Matcher hasFailureContaining(final String string) { return new TypeSafeMatcher() { @Override public boolean matchesSafely(PrintableResult item) { return item.failureCount() > 0 && item.toString().contains(string); } public void describeTo(Description description) { description.appendText("has failure containing " + string); } }; } }