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