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

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

Go to download

JUnit is a regression testing framework written by Erich Gamma and Kent Beck. It is used by the developer who implements unit tests in Java.

There is a newer version: 4.11
Show newest version
package org.junit.experimental.results;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.internal.matchers.TypeSafeMatcher;

public class ResultMatchers {
	public static Matcher isSuccessful() {
		return failureCountIs(0);
	}

	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.getFailures().size() == count;
			}
		};
	}
	
	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);
			}
		};
	}
	
	public static Matcher hasFailureContaining(final String string) {
		return new BaseMatcher() {
			public boolean matches(Object item) {
				return item.toString().contains(string);
			}

			public void describeTo(Description description) {
				description.appendText("has failure containing " + string);
			}
		};
	}
}