SpringBootでSpringMVCをテストするMockMVCについて、テストメソッドをまとめました。(作成中)
MockMvcResultMatchersの使い方まとめです。
バージョンは5.0.4(spring-test-5.0.4.RELEASE.jar)。
■MockMvcResultMatchers
| メソッド名 | 内容 |
| public static RequestResultMatchers request() | リクエスト関連を検証する |
| public static HandlerResultMatchers handler() | ハンドラ(Controllerクラス)を検証する |
| public static ModelResultMatchers model() | モデル関連を検証する |
| public static ViewResultMatchers view() | 選択されたビューを検証する |
| public static FlashAttributeResultMatchers flash() | flash属性(flashスコープ:リダイレクト時も使用できるセッションのような一時的な記憶領域)を検証する |
| public static ResultMatcher forwardedUrl(String expectedUrl) | 遷移先URLを検証する |
実行例)遷移先URLが"index"
MockMvcオブジェクト.perform(get("/")
.andExpect(forwardedUrl("index"));
|
|
| public static ResultMatcher forwardedUrlTemplate(String urlTemplate, Object… uriVars) | 遷移先URLを可変パターンで検証する |
実行例)遷移先URLが"index"
MockMvcオブジェクト.perform(get("/")
.andExpect(forwardedUrlTemplate("in{value}e{value2}", "d", "x"));
|
|
| public static ResultMatcher forwardedUrlPattern(String urlPattern) | 遷移先URLをパターンで検証する |
実行例)遷移先URLが"in(何らかの文字(なくてもよい))"
MockMvcオブジェクト.perform(get("/")
.andExpect(forwardedUrlPattern("in*"));
|
|
実行例)遷移先URLが"in(何らかの1文字)ex"
MockMvcオブジェクト.perform(get("/")
.andExpect(forwardedUrlPattern("in?ex"));
|
|
| public static ResultMatcher redirectedUrl(String expectedUrl) | リダイレクト先URLを検証する |
実行例)リダイレクトURLが"index"
MockMvcオブジェクト.perform(get("/")
.andExpect(redirectedUrl("index"));
|
|
| public static ResultMatcher redirectedUrlTemplate(String urlTemplate, Object… uriVars) | リダイレクト先URLを可変パターンで検証する |
実行例)リダイレクトURLが"in(何らかの文字(なくてもよい))"
MockMvcオブジェクト.perform(get("/")
.andExpect(redirectedUrlPattern("in*"));
|
|
実行例)リダイレクトURLが"in(何らかの1文字)ex"
MockMvcオブジェクト.perform(get("/")
.andExpect(redirectedUrlPattern("in?ex"));
|
|
| public static ResultMatcher redirectedUrlPattern(String urlPattern) | リダイレクト先URLをパターンで検証する |
実行例)リダイレクトURLが"index"
MockMvcオブジェクト.perform(get("/")
.andExpect(redirectedUrlTemplate("in{value}e{value2}", "d", "x"));
|
|
| public static StatusResultMatchers status() | HTTPレスポンスのステータスコード、エラーメッセージを検証する |
| public static HeaderResultMatchers header() | レスポンスヘッダを検証する |
| public static ContentResultMatchers content() | レスポンスボディを検証する |
| public static JsonPathResultMatchers jsonPath(String expression, Object … args) | |
| public static |
|
| public static XpathResultMatchers xpath(String expression, Object… args) throws XPathExpressionException | |
| public static XpathResultMatchers xpath(String expression, Map |
|
| public static CookieResultMatchers cookie() | |