|
| 1 | +package com.baeldung.mvc.velocity.test; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.allOf; |
| 4 | +import static org.hamcrest.Matchers.hasItem; |
| 5 | +import static org.hamcrest.Matchers.hasProperty; |
| 6 | +import static org.hamcrest.Matchers.hasSize; |
| 7 | +import static org.hamcrest.Matchers.is; |
| 8 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 9 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; |
| 12 | + |
| 13 | +import java.util.Arrays; |
| 14 | + |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.mockito.Mockito; |
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.test.context.ContextConfiguration; |
| 21 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 22 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 23 | +import org.springframework.test.web.servlet.MockMvc; |
| 24 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 25 | +import org.springframework.web.context.WebApplicationContext; |
| 26 | + |
| 27 | +import com.baeldung.mvc.velocity.domain.Tutorial; |
| 28 | +import com.baeldung.mvc.velocity.service.ITutorialsService; |
| 29 | +import com.baeldung.mvc.velocity.service.TutorialsService; |
| 30 | + |
| 31 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 32 | +@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) |
| 33 | +@WebAppConfiguration |
| 34 | +public class DataContentControllerTest { |
| 35 | + |
| 36 | + |
| 37 | + private MockMvc mockMvc; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private ITutorialsService tutServiceMock; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private WebApplicationContext webApplicationContext; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setUp() { |
| 47 | + tutServiceMock = Mockito.mock(TutorialsService.class); |
| 48 | + Mockito.reset(tutServiceMock); |
| 49 | + |
| 50 | + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testModel() throws Exception{ |
| 55 | + |
| 56 | + |
| 57 | + Mockito.when(tutServiceMock.listTutorials()).thenReturn(Arrays.asList( |
| 58 | + new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), |
| 59 | + new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor") |
| 60 | + )); |
| 61 | + |
| 62 | + mockMvc.perform(get("/")) |
| 63 | + .andExpect(status().isOk()) |
| 64 | + .andExpect(view().name("index")) |
| 65 | + .andExpect(model().attribute("tutorials", hasSize(2))) |
| 66 | + .andExpect(model().attribute("tutorials", hasItem( |
| 67 | + allOf( |
| 68 | + hasProperty("tutId", is(1)), |
| 69 | + hasProperty("author", is("GuavaAuthor")), |
| 70 | + hasProperty("title", is("Guava")) |
| 71 | + ) |
| 72 | + ))) |
| 73 | + .andExpect(model().attribute("tutorials", hasItem( |
| 74 | + allOf( |
| 75 | + hasProperty("tutId", is(2)), |
| 76 | + hasProperty("author", is("AndroidAuthor")), |
| 77 | + hasProperty("title", is("Android")) |
| 78 | + ) |
| 79 | + ))); |
| 80 | + } |
| 81 | +} |
0 commit comments