Skip to content

Commit

Permalink
Fix ResultMatchers#hasFailureContaining (#1292)
Browse files Browse the repository at this point in the history
ResultMatchers.hasFailureContaining() should not match when the given PrintableResult has no failures.
  • Loading branch information
alb-i986 authored and kcooney committed Sep 18, 2016
1 parent 93bcc8d commit 3637550
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public void describeTo(Description description) {
* contains {@code string}
*/
public static Matcher<PrintableResult> hasFailureContaining(final String string) {
return new BaseMatcher<PrintableResult>() {
public boolean matches(Object item) {
return item.toString().contains(string);
return new TypeSafeMatcher<PrintableResult>() {
public boolean matchesSafely(PrintableResult item) {
return item.failureCount() > 0 && item.toString().contains(string);
}

public void describeTo(Description description) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package org.junit.tests.experimental.results;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.experimental.results.PrintableResult;
import org.junit.experimental.results.ResultMatchers;
import org.junit.experimental.theories.Theory;
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;

import java.util.ArrayList;
import java.util.Collections;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class ResultMatchersTest {

@Test
public void hasFailuresHasGoodDescription() {
assertThat(ResultMatchers.failureCountIs(3).toString(),
Expand All @@ -20,4 +27,20 @@ public void hasFailuresDescriptionReflectsInput(int i) {
assertThat(ResultMatchers.failureCountIs(i).toString(),
containsString("" + i));
}

@Test
public void hasFailureContaining_givenResultWithNoFailures() {
PrintableResult resultWithNoFailures = new PrintableResult(new ArrayList<Failure>());

assertThat(ResultMatchers.hasFailureContaining("").matches(resultWithNoFailures), is(false));
}

@Test
public void hasFailureContaining_givenResultWithOneFailure() {
PrintableResult resultWithOneFailure = new PrintableResult(Collections.singletonList(
new Failure(Description.EMPTY, new RuntimeException("my failure"))));

assertThat(ResultMatchers.hasFailureContaining("my failure").matches(resultWithOneFailure), is(true));
assertThat(ResultMatchers.hasFailureContaining("his failure").matches(resultWithOneFailure), is(false));
}
}

0 comments on commit 3637550

Please sign in to comment.