-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from mbland/test-matchers-and-other-tweaks
Hoist up Node Gradle plugin, set TestTomcat baseDir, add hasContentType matcher, HTTP status consts
- Loading branch information
Showing
6 changed files
with
76 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
strcalc/src/test/java/com/mike_bland/training/testing/matchers/HasContentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.mike_bland.training.testing.matchers; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.TypeSafeMatcher; | ||
|
||
import java.net.http.HttpResponse; | ||
|
||
// Custom Hamcrest matcher validating the Content-Type header of a HttpResponse. | ||
// | ||
// This could be generalized to check any HTTP header, or collection thereof. | ||
// However, since this is a teaching example, we'll keep it straightforward. | ||
class HasContentType<T> extends TypeSafeMatcher<HttpResponse<T>> { | ||
private final String expected; | ||
|
||
// Constructor to register the expected Content-Type value. | ||
HasContentType(String contentType) { | ||
this.expected = contentType; | ||
} | ||
|
||
// Helper method to extract the Content-Type value from an HttpResponse. | ||
private String getContentType(HttpResponse<T> resp) { | ||
return resp.headers().firstValue("Content-Type").orElse(""); | ||
} | ||
|
||
// Performs the actual assertion. | ||
@Override public boolean matchesSafely(HttpResponse<T> resp) { | ||
return getContentType(resp).equals(expected); | ||
} | ||
|
||
// Describes the "Expected:" value in assertion failure messages. | ||
@Override public void describeTo(Description description) { | ||
description.appendText(expected); | ||
} | ||
|
||
// Describes the actual ("but:") value in assertion failure messages. | ||
@Override public void describeMismatchSafely( | ||
HttpResponse<T> resp, Description description) { | ||
description.appendText(getContentType(resp)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
strcalc/src/test/java/com/mike_bland/training/testing/matchers/Matchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.mike_bland.training.testing.matchers; | ||
|
||
import org.hamcrest.Matcher; | ||
|
||
import java.net.http.HttpResponse; | ||
|
||
// Collection of custom Hamcrest Matcher<T> classes for assertThat() statements. | ||
// | ||
// These tutorials describe how to write Matchers: | ||
// | ||
// - https://hamcrest.org/JavaHamcrest/tutorial | ||
// - https://www.baeldung.com/hamcrest-custom-matchers | ||
// | ||
// Note that these tutorials show the static factory functions defined on the | ||
// same class as the Matcher. However, Hamcrest itself collects these factories | ||
// into its own org.hamcrest.Matchers class for convenience, instead of | ||
// importing one class per Matcher. | ||
public class Matchers { | ||
public static <T> Matcher<HttpResponse<T>> hasContentType( | ||
String contentType) { | ||
return new HasContentType<>(contentType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters