Setting Up the MVC Test Context

Notice that the builders generate the MVC context with the build() method.

Mocking from a Controller

private final UserController getController() {
	final UserService service; // Mocked service

	service = Mockito.mock(UserService.class);

	return new UserController(service);
}
mockMvc = MockMvcBuilders.standaloneSetup(getController()).build();

Mocking from Application Context

@Resource
private WebApplicationContext webApplicationContext;
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

Last updated