From 0ee6f4faed75f0b3554845c647d92ca1ff0e57c8 Mon Sep 17 00:00:00 2001 From: Miguel Reboiro-Jato Date: Fri, 2 Mar 2018 11:34:05 +0100 Subject: [PATCH] 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. --- pom.xml | 2 +- .../esei/daa/matchers/HasHttpStatus.java | 27 +++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index a52db1f..57fc0f5 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ es.uvigo.esei.daa example war - 0.1.8 + 0.1.9 DAA Example diff --git a/src/test/java/es/uvigo/esei/daa/matchers/HasHttpStatus.java b/src/test/java/es/uvigo/esei/daa/matchers/HasHttpStatus.java index e5e7111..8da8cb1 100644 --- a/src/test/java/es/uvigo/esei/daa/matchers/HasHttpStatus.java +++ b/src/test/java/es/uvigo/esei/daa/matchers/HasHttpStatus.java @@ -8,12 +8,12 @@ * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public * License along with this program. If not, see * . @@ -34,11 +34,11 @@ import org.hamcrest.TypeSafeMatcher; public class HasHttpStatus extends TypeSafeMatcher { private final StatusType expectedStatus; - + public HasHttpStatus(StatusType expectedStatus) { this.expectedStatus = requireNonNull(expectedStatus); } - + public HasHttpStatus(int expectedStatus) { this(Status.fromStatusCode(expectedStatus)); } @@ -48,41 +48,46 @@ public class HasHttpStatus extends TypeSafeMatcher { description.appendValue(this.expectedStatus); } + @Override + protected void describeMismatchSafely(Response item, Description mismatchDescription) { + mismatchDescription.appendText("was ").appendValue(item.getStatusInfo()); + } + @Override protected boolean matchesSafely(Response item) { return item != null && expectedStatus.getStatusCode() == item.getStatusInfo().getStatusCode(); } - + @Factory public static Matcher hasHttpStatus(StatusType expectedStatus) { return new HasHttpStatus(expectedStatus); } - + @Factory public static Matcher hasHttpStatus(int expectedStatus) { return new HasHttpStatus(expectedStatus); } - + @Factory public static Matcher hasOkStatus() { return new HasHttpStatus(Response.Status.OK); } - + @Factory public static Matcher hasBadRequestStatus() { return new HasHttpStatus(Response.Status.BAD_REQUEST); } - + @Factory public static Matcher hasInternalServerErrorStatus() { return new HasHttpStatus(Response.Status.INTERNAL_SERVER_ERROR); } - + @Factory public static Matcher hasUnauthorized() { return new HasHttpStatus(Response.Status.UNAUTHORIZED); } - + @Factory public static Matcher hasForbidden() { return new HasHttpStatus(Response.Status.FORBIDDEN); -- 2.18.1