I really like REST assured, but I've noticed that the error messages when a matcher fails sometimes aren't as good as they could be.
For example:
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsInAnyOrder;
import java.util.Collection;
import org.junit.Test;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.specification.RequestSpecification;
public class RATest {
RequestSpecification reqSpec = new RequestSpecBuilder()
.setBaseUri("http://httpbin.org")
.setPort(80)
.addParam("arg1", "val1")
.addParam("arg2", "val2")
.addParam("arg3", "val3")
.build();
@Test
public void raAssert() {
given(reqSpec)
.when() .get("get")
.then() .body("args.values()", containsInAnyOrder("val1", "valtwo", "val3"));
}
@Test
public void junitAssert() {
Collection<String> argValue =
given(reqSpec)
.when() .get("get")
.then() .extract().path("args.values()");
org.hamcrest.MatcherAssert.assertThat(argValue, containsInAnyOrder("val1", "valtwo", "val3"));
}
} The first test fails with the following exception:
java.lang.AssertionError: JSON path args.values() doesn't match.
Expected: iterable over ["val1", "valtwo", "val3"] in any order
Actual: [val3, val2, val1]
and the second test fails with this exception:
java.lang.AssertionError:
Expected: iterable over ["val1", "valtwo", "val3"] in any order
but: Not matched: "val2"
The second message is much better and points directly to the issue- imagine if the list was 100 items rather than just three.
There's nothing fancy in Hamcrest's MatcherAssert:
| public static <T> void assertThat(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); |
|
| throw new AssertionError(description.toString()); |
| } |
| } |
Maybe you can reproduce that logic or call it directly? I think there are probably a lot of good matcher error messages that we're missing out on.
Thanks,
-Brian