Commit 0ee6f4fa authored by Administrator's avatar Administrator

Modifies HasHttpStatus matcher for a more descriptive message

The message showed currently by the HasHttpStatus when de expected
status didn't match the actual status was not very informative, as
the actual status was not shown in the message.

This commit modifies this behaviour to show both status (expected and
actual) in the message.
parent 3fd80931
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<groupId>es.uvigo.esei.daa</groupId> <groupId>es.uvigo.esei.daa</groupId>
<artifactId>example</artifactId> <artifactId>example</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<version>0.1.8</version> <version>0.1.9</version>
<name>DAA Example</name> <name>DAA Example</name>
<licenses> <licenses>
......
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
* it under the terms of the GNU General Public License as * it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version. * License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public * You should have received a copy of the GNU General Public
* License along with this program. If not, see * License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>. * <http://www.gnu.org/licenses/gpl-3.0.html>.
...@@ -34,11 +34,11 @@ import org.hamcrest.TypeSafeMatcher; ...@@ -34,11 +34,11 @@ import org.hamcrest.TypeSafeMatcher;
public class HasHttpStatus extends TypeSafeMatcher<Response> { public class HasHttpStatus extends TypeSafeMatcher<Response> {
private final StatusType expectedStatus; private final StatusType expectedStatus;
public HasHttpStatus(StatusType expectedStatus) { public HasHttpStatus(StatusType expectedStatus) {
this.expectedStatus = requireNonNull(expectedStatus); this.expectedStatus = requireNonNull(expectedStatus);
} }
public HasHttpStatus(int expectedStatus) { public HasHttpStatus(int expectedStatus) {
this(Status.fromStatusCode(expectedStatus)); this(Status.fromStatusCode(expectedStatus));
} }
...@@ -48,41 +48,46 @@ public class HasHttpStatus extends TypeSafeMatcher<Response> { ...@@ -48,41 +48,46 @@ public class HasHttpStatus extends TypeSafeMatcher<Response> {
description.appendValue(this.expectedStatus); description.appendValue(this.expectedStatus);
} }
@Override
protected void describeMismatchSafely(Response item, Description mismatchDescription) {
mismatchDescription.appendText("was ").appendValue(item.getStatusInfo());
}
@Override @Override
protected boolean matchesSafely(Response item) { protected boolean matchesSafely(Response item) {
return item != null && expectedStatus.getStatusCode() == item.getStatusInfo().getStatusCode(); return item != null && expectedStatus.getStatusCode() == item.getStatusInfo().getStatusCode();
} }
@Factory @Factory
public static Matcher<Response> hasHttpStatus(StatusType expectedStatus) { public static Matcher<Response> hasHttpStatus(StatusType expectedStatus) {
return new HasHttpStatus(expectedStatus); return new HasHttpStatus(expectedStatus);
} }
@Factory @Factory
public static Matcher<Response> hasHttpStatus(int expectedStatus) { public static Matcher<Response> hasHttpStatus(int expectedStatus) {
return new HasHttpStatus(expectedStatus); return new HasHttpStatus(expectedStatus);
} }
@Factory @Factory
public static Matcher<Response> hasOkStatus() { public static Matcher<Response> hasOkStatus() {
return new HasHttpStatus(Response.Status.OK); return new HasHttpStatus(Response.Status.OK);
} }
@Factory @Factory
public static Matcher<Response> hasBadRequestStatus() { public static Matcher<Response> hasBadRequestStatus() {
return new HasHttpStatus(Response.Status.BAD_REQUEST); return new HasHttpStatus(Response.Status.BAD_REQUEST);
} }
@Factory @Factory
public static Matcher<Response> hasInternalServerErrorStatus() { public static Matcher<Response> hasInternalServerErrorStatus() {
return new HasHttpStatus(Response.Status.INTERNAL_SERVER_ERROR); return new HasHttpStatus(Response.Status.INTERNAL_SERVER_ERROR);
} }
@Factory @Factory
public static Matcher<Response> hasUnauthorized() { public static Matcher<Response> hasUnauthorized() {
return new HasHttpStatus(Response.Status.UNAUTHORIZED); return new HasHttpStatus(Response.Status.UNAUTHORIZED);
} }
@Factory @Factory
public static Matcher<Response> hasForbidden() { public static Matcher<Response> hasForbidden() {
return new HasHttpStatus(Response.Status.FORBIDDEN); return new HasHttpStatus(Response.Status.FORBIDDEN);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment