diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java index 2c8f224f8c..679a455f3f 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java @@ -10,21 +10,33 @@ import org.springframework.web.bind.annotation.RequestMethod; import java.util.List; -@Controller @RequestMapping("/") public class MainController { +@Controller +@RequestMapping("/") +public class MainController { - private final ITutorialsService tutService; + @Autowired + private ITutorialsService tutService; - @Autowired public MainController(ITutorialsService tutService) { - this.tutService = tutService; + @RequestMapping(value ="/", method = RequestMethod.GET) + public String welcomePage() { + return "index"; } - - @RequestMapping(method = RequestMethod.GET) public String listTutorialsPage(Model model) { + + + @RequestMapping(value ="/list", method = RequestMethod.GET) + public String listTutorialsPage(Model model) { List list = tutService.listTutorials(); model.addAttribute("tutorials", list); - return "index"; + return "list"; } public ITutorialsService getTutService() { return tutService; } + + public void setTutService(ITutorialsService tutService) { + this.tutService = tutService; + } + + } \ No newline at end of file diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java index 4987ca1ada..6903a662b9 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java @@ -13,11 +13,8 @@ import java.util.Set; public class MainWebAppInitializer implements WebApplicationInitializer { - /** - * Register and configure all Servlet container components necessary to - * power the web application. - */ - @Override public void onStartup(final ServletContext sc) throws ServletException { + @Override + public void onStartup(final ServletContext sc) throws ServletException { // Create the 'root' Spring application context final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java index 99fc99be80..017e1941f1 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java @@ -1,13 +1,10 @@ package com.baeldung.mvc.velocity.spring.config; -import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; -import com.baeldung.mvc.velocity.service.TutorialsService; +@Configuration +@ComponentScan(basePackages = { "com.baeldung.mvc.velocity.service" }) +public class SpringConfig { -@Configuration public class SpringConfig { - - @Bean public TutorialsService tutService() { - return new TutorialsService(); - } } diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java index 1a0f5742e4..1a3ec62de2 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java @@ -11,21 +11,23 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter import org.springframework.web.servlet.view.velocity.VelocityConfigurer; import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver; -@Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.baeldung.mvc.velocity.controller" }) public class WebConfig extends WebMvcConfigurerAdapter { +@Configuration +@EnableWebMvc +@ComponentScan(basePackages = { "com.baeldung.mvc.velocity.controller"}) +public class WebConfig extends WebMvcConfigurerAdapter { - public WebConfig() { - super(); - } - - @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } - @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } - @Bean public ViewResolver viewResolver() { + @Bean + public ViewResolver viewResolver() { final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver(); bean.setCache(true); bean.setPrefix("/WEB-INF/views/"); @@ -34,7 +36,8 @@ import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver; return bean; } - @Bean public VelocityConfigurer velocityConfig() { + @Bean + public VelocityConfigurer velocityConfig() { VelocityConfigurer velocityConfigurer = new VelocityConfigurer(); velocityConfigurer.setResourceLoaderPath("/"); return velocityConfigurer; diff --git a/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm b/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm index 9e06a09e4f..8883a50658 100644 --- a/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm +++ b/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm @@ -1,19 +1,4 @@

Index

-

Tutorials list

- - - - - - - -#foreach($tut in $tutorials) - - - - - - -#end -
Tutorial IdTutorial TitleTutorial DescriptionTutorial Author
$tut.tutId$tut.title$tut.description$tut.author
\ No newline at end of file +

Welcome page

+This is just the welcome page \ No newline at end of file diff --git a/spring-mvc-velocity/src/main/webapp/WEB-INF/views/list.vm b/spring-mvc-velocity/src/main/webapp/WEB-INF/views/list.vm new file mode 100644 index 0000000000..9e06a09e4f --- /dev/null +++ b/spring-mvc-velocity/src/main/webapp/WEB-INF/views/list.vm @@ -0,0 +1,19 @@ +

Index

+ +

Tutorials list

+ + + + + + + +#foreach($tut in $tutorials) + + + + + + +#end +
Tutorial IdTutorial TitleTutorial DescriptionTutorial Author
$tut.tutId$tut.title$tut.description$tut.author
\ No newline at end of file diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java index b766075f8a..6b6794a653 100644 --- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java +++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java @@ -31,43 +31,40 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @RunWith(SpringJUnit4ClassRunner.class) // @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) -@ContextConfiguration(classes = { TestConfig.class, WebConfig.class }) @WebAppConfiguration public class DataContentControllerTest { +@ContextConfiguration(classes = { TestConfig.class, WebConfig.class }) +@WebAppConfiguration +public class DataContentControllerTest { private MockMvc mockMvc; - @Autowired private ITutorialsService tutServiceMock; + @Autowired + private ITutorialsService tutServiceMock; - @Autowired private WebApplicationContext webApplicationContext; + @Autowired + private WebApplicationContext webApplicationContext; - @Before public void setUp() { + @Before + public void setUp() { Mockito.reset(tutServiceMock); mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } - @Test public void testModel() throws Exception { + @Test + public void whenCallingList_ThenModelAndContentOK() throws Exception { - Mockito.when(tutServiceMock.listTutorials()) - .thenReturn(Arrays.asList( - new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), - new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor"))); + Mockito.when(tutServiceMock.listTutorials()).thenReturn(Arrays.asList(new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor"))); - mockMvc.perform(get("/")) - .andExpect(status().isOk()).andExpect(view().name("index")) - .andExpect(model().attribute("tutorials", hasSize(2))) - .andExpect(model() - .attribute("tutorials", - hasItem(allOf(hasProperty("tutId", is(1)), - hasProperty("author", is("GuavaAuthor")), - hasProperty("title", is("Guava")))))) - .andExpect(model() - .attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)), - hasProperty("author", is("AndroidAuthor")), - hasProperty("title", is("Android")))))); + mockMvc.perform(get("/list")).andExpect(status().isOk()).andExpect(view().name("list")).andExpect(model().attribute("tutorials", hasSize(2))) + .andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(1)), hasProperty("author", is("GuavaAuthor")), hasProperty("title", is("Guava")))))) + .andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)), hasProperty("author", is("AndroidAuthor")), hasProperty("title", is("Android")))))); - mockMvc.perform(get("/")) - .andExpect(xpath("//table").exists()); - mockMvc.perform(get("/")) - .andExpect(xpath("//td[@id='tutId_1']").exists()); + mockMvc.perform(get("/list")).andExpect(xpath("//table").exists()); + mockMvc.perform(get("/list")).andExpect(xpath("//td[@id='tutId_1']").exists()); + } + + @Test + public void whenCallingIndex_thenViewOK() throws Exception{ + mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index")).andExpect(model().size(0)); } } diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java deleted file mode 100644 index 0189086153..0000000000 --- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.baeldung.mvc.velocity.test; - -import com.baeldung.mvc.velocity.controller.MainController; -import com.baeldung.mvc.velocity.domain.Tutorial; -import com.baeldung.mvc.velocity.service.TutorialsService; -import com.baeldung.mvc.velocity.test.config.TestConfig; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.ui.ExtendedModelMap; -import org.springframework.ui.Model; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - -@RunWith(SpringJUnit4ClassRunner.class) -@WebAppConfiguration -@ContextConfiguration(classes = { TestConfig.class }) -public class NavigationControllerTest { - - private MainController mainController = new MainController(Mockito.mock(TutorialsService.class)); - - private final Model model = new ExtendedModelMap(); - - @Test public void shouldGoToTutorialListView() { - Mockito.when(mainController.getTutService().listTutorials()).thenReturn(createTutorialList()); - - final String view = mainController.listTutorialsPage(model); - final List tutorialListAttribute = (List) model.asMap().get("tutorials"); - - assertEquals("index", view); - assertNotNull(tutorialListAttribute); - } - - @Test public void testContent() throws Exception { - - List tutorials = Arrays.asList( - new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), - new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")); - - Mockito.when(mainController.getTutService().listTutorials()) - .thenReturn(tutorials); - - String view = mainController.listTutorialsPage(model); - - verify(mainController.getTutService(), times(1)).listTutorials(); - verifyNoMoreInteractions(mainController.getTutService()); - - assertEquals("index", view); - assertEquals(tutorials, model.asMap().get("tutorials")); - } - - private static List createTutorialList() { - return Arrays.asList(new Tutorial(1, "TestAuthor", "Test Title", "Test Description")); - } -}