Mocks and stubs

If we wanted to test the search request handled by the SearchController class, we would certainly want to mock SearchService.

There are two ways of doing this: with a mock or with a stub.

Mocking with Mockito

First, we can create a mock object with Mockito:

package masterSpringMvc.search; import masterSpringMvc.MasterSpringMvcApplication; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; ...

Get Spring MVC: Designing Real-World Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.