11package 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 ;
723import org .junit .Test ;
824import org .junit .runner .RunWith ;
925import 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 ;
1132import org .springframework .ui .ExtendedModelMap ;
1233import 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" })
2248public 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