io.github.olib963.javatest.matchers.internal.ThrowsExceptionMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javatest-matchers Show documentation
Show all versions of javatest-matchers Show documentation
Matchers to create assertions from common conditions
package io.github.olib963.javatest.matchers.internal;
import io.github.olib963.javatest.matchers.CheckedRunnable;
import io.github.olib963.javatest.matchers.MatchResult;
import io.github.olib963.javatest.matchers.Matcher;
public class ThrowsExceptionMatcher implements Matcher {
private final Matcher thrownMatcher;
public ThrowsExceptionMatcher(Matcher thrownMatcher) {
this.thrownMatcher = thrownMatcher;
}
@Override
public MatchResult matches(CheckedRunnable value) {
try {
value.run();
return MatchResult.mismatch("no exception was thrown");
} catch (Exception e) {
return thrownMatcher.matches(e);
}
}
@Override
public String describeExpected() {
return "throw an exception that was expected to " + thrownMatcher.describeExpected();
}
}