Skip to content

Commit fc7ea87

Browse files
committed
Test Model Content
1 parent 6b99ced commit fc7ea87

6 files changed

Lines changed: 93 additions & 13 deletions

File tree

‎spring-mvc-velocity/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
<version>${powermock.version}</version>
9696
<scope>test</scope>
9797
</dependency>
98+
<dependency>
99+
<groupId>org.springframework</groupId>
100+
<artifactId>spring-test</artifactId>
101+
<version>${org.springframework.version}</version>
102+
<scope>test</scope>
103+
</dependency>
98104

99105
</dependencies>
100106

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212

1313
@Controller
14+
@RequestMapping("/")
1415
public class MainController {
1516

1617
private final TutorialsService tutService;

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/domain/Tutorial.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,22 @@ public Tutorial(Integer tutId, String title, String description, String author)
1313
this.description = description;
1414
this.author = author;
1515
}
16+
17+
public Integer getTutId() {
18+
return tutId;
19+
}
20+
21+
public String getTitle() {
22+
return title;
23+
}
24+
25+
public String getDescription() {
26+
return description;
27+
}
28+
29+
public String getAuthor() {
30+
return author;
31+
}
32+
33+
1634
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.mvc.velocity.service;
2+
3+
import java.util.List;
4+
5+
import com.baeldung.mvc.velocity.domain.Tutorial;
6+
7+
public interface ITutorialsService {
8+
9+
public List<Tutorial> listTutorials();
10+
}

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/TutorialsService.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.List;
88

99
@Service
10-
public class TutorialsService {
10+
public class TutorialsService implements ITutorialsService {
1111

1212
public List<Tutorial> listTutorials() {
1313
return Arrays.asList(

‎spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java‎

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,59 @@
11
package com.baeldung.mvc.velocity.test;
22

33

4-
import com.baeldung.mvc.velocity.controller.MainController;
5-
import com.baeldung.mvc.velocity.domain.Tutorial;
6-
import com.baeldung.mvc.velocity.service.TutorialsService;
4+
import static org.hamcrest.Matchers.allOf;
5+
import static org.hamcrest.Matchers.hasItem;
6+
import static org.hamcrest.Matchers.hasProperty;
7+
import static org.hamcrest.Matchers.is;
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.mockito.Mockito.times;
11+
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.verifyNoMoreInteractions;
13+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
15+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
import org.junit.Before;
723
import org.junit.Test;
824
import org.junit.runner.RunWith;
925
import org.mockito.Mockito;
10-
import org.mockito.runners.MockitoJUnitRunner;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
29+
import org.springframework.test.context.web.WebAppConfiguration;
30+
import org.springframework.test.web.servlet.MockMvc;
31+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1132
import org.springframework.ui.ExtendedModelMap;
1233
import org.springframework.ui.Model;
34+
import org.springframework.web.context.WebApplicationContext;
35+
import org.springframework.web.servlet.ViewResolver;
36+
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
1337

14-
import java.util.Arrays;
15-
import java.util.List;
16-
17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertNotNull;
38+
import com.baeldung.mvc.velocity.config.TestContext;
39+
import com.baeldung.mvc.velocity.controller.MainController;
40+
import com.baeldung.mvc.velocity.domain.Tutorial;
41+
import com.baeldung.mvc.velocity.service.ITutorialsService;
42+
import com.baeldung.mvc.velocity.service.TutorialsService;
1943

2044

21-
@RunWith(MockitoJUnitRunner.class)
45+
@RunWith(SpringJUnit4ClassRunner.class)
46+
@WebAppConfiguration
47+
@ContextConfiguration(locations={"classpath:mvc-servlet.xml"})
2248
public class NavigationControllerTest {
2349

24-
private final MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
50+
private MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
2551

2652
private final Model model = new ExtendedModelMap();
53+
2754

2855
@Test
29-
public final void shouldGoToTutorialListView() {
56+
public void shouldGoToTutorialListView() {
3057
Mockito.when(mainController.getTutService().listTutorials())
3158
.thenReturn(createTutorialList());
3259

@@ -36,6 +63,24 @@ public final void shouldGoToTutorialListView() {
3663
assertEquals("index", view);
3764
assertNotNull(tutorialListAttribute);
3865
}
66+
67+
@Test
68+
public void testContent() throws Exception{
69+
70+
List<Tutorial> tutorials = Arrays.asList(
71+
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
72+
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")
73+
);
74+
Mockito.when(mainController.getTutService().listTutorials()).thenReturn(tutorials);
75+
76+
String view = mainController.listTutorialsPage(model);
77+
78+
verify(mainController.getTutService(), times(1)).listTutorials();
79+
verifyNoMoreInteractions(mainController.getTutService());
80+
81+
assertEquals("index", view);
82+
assertEquals(tutorials, model.asMap().get("tutorials"));
83+
}
3984

4085

4186
private static List<Tutorial> createTutorialList() {

0 commit comments

Comments
 (0)