2
4

More than 3 years have passed since last update.

Spring Boot のコントローラーを MockMvc でユニットテストするサンプルコード

Posted at

今回の環境

  • Java 11 (OpenJDK 11.0.2)
  • Spring Boot 2.2.2
  • Thymeleaf 3.0.11
  • JUnit 5.5.2
  • Hamcrest 2.1

テスト対象のコントローラーと Thymeleaf テンプレート

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FooBarController {

  @GetMapping("/")
  public ModelAndView topPage(ModelAndView mav) {
    mav.setViewName("index");
    mav.addObject("userName", "Alice");
    return mav;
  }
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>My App</title>
</head>
<body>
<p id="user-name" th:text="${userName}"></p>
</body>
</html>

コントローラーをテストするクラス

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@SpringBootTest
@AutoConfigureMockMvc
public class FooBarControllerTest {

  @Autowired
  private MockMvc mockMvc;

  // ユーザーエージェント文字列
  private static final String USER_AGENT =
    "Mozilla/5.0 (iPhone; CPU iPhone OS 13_0 like Mac OS X) " +
    "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 " +
    "Mobile/15E148 Safari/604.1";

  @Test
  public void testTopPage() throws Exception {
    this.mockMvc.perform(
      // GET メソッドでアクセス
      get("/")
      // リクエストヘッダを指定
      .header(HttpHeaders.USER_AGENT, USER_AGENT))
      // HTTP ステータスコードをテスト
      .andExpect(status().is2xxSuccessful())
      // ビュー名をテスト
      .andExpect(view().name("index"))
      // モデルの属性をテスト
      .andExpect(model().attribute("userName", "Alice"))
      // Content Type をテスト
      .andExpect(content().contentType("text/html;charset=UTF-8"))
      // ページに指定したテキストが含まれるかテスト
      .andExpect(content().string(containsString("Alice")));
  }
}

参考資料

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4