Skip to content

Instantly share code, notes, and snippets.

@cescoffier
Created January 15, 2016 09:14
Show Gist options
  • Save cescoffier/5cbf4c69aa094ac9b1a6 to your computer and use it in GitHub Desktop.
Save cescoffier/5cbf4c69aa094ac9b1a6 to your computer and use it in GitHub Desktop.
Provide `assertThat` method that can be used with Vert.x Unit.
package io.vertx.unit.example;
import io.vertx.ext.unit.TestContext;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
/**
* Provides {@code assertThat} methods usable with Vertx-Unit.
*/
public class VertxMatcherAssert {
/**
* Checks that {@code actual} matches {@code matchers}. If not, the error is reported on the given {@code
* TestContext}.
*
* @param context the test context
* @param actual the object to test
* @param matcher the matcher
* @param <T> the type of the object to test
*/
public static <T> void assertThat(TestContext context, T actual, Matcher<? super T> matcher) {
assertThat(context, "", actual, matcher);
}
/**
* Checks that {@code actual} matches {@code matchers}. If not, the error is reported on the given {@code
* TestContext}.
*
* @param context the test context
* @param reason the reason
* @param actual the object to test
* @param matcher the matcher
* @param <T> the type of the object to test
*/
public static <T> void assertThat(TestContext context, String reason, T actual, Matcher<? super T> matcher) {
if (!matcher.matches(actual)) {
Description description = new StringDescription();
description.appendText(reason)
.appendText("\nExpected: ")
.appendDescriptionOf(matcher)
.appendText("\n but: ");
matcher.describeMismatch(actual, description);
context.fail(description.toString());
}
}
/**
* Checks whether or not {@code assertion} is {@code true}. If not, it reports the error on the given {@code
* TestContext}.
*
* @param context the test context
* @param reason the reason
* @param assertion the assertion state
*/
public static void assertThat(TestContext context, String reason, boolean assertion) {
if (!assertion) {
context.fail(reason);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment