Testing Spring MVC Web Controller

Overview

Problem

  • How can we test Spring MVC Web Controllers?
  • How can we create HTTP requests and send to the controller?
  • How can we verify the response? status_code, view_name, model_attributes.

Spring Testing support

  • Mock object support for web, REST APIs etc.
  • For testing controller, we can use MockMvc.
  • Provides Spring MVC processing of request/response.
  • There is no need to run a server(embadded or external).

Development process

  • Add annotation @AutoConfigureMockMvc
  • Inject the MockMvc
  • Perform web request
  • Define expections
  • Assert results