Created view without data (#537)

* Remove unnecessary code from Velocity example

* view without data
This commit is contained in:
Raquel Garrido 2016-07-27 19:23:00 +02:00 committed by Grzegorz Piwowarek
parent e694a08827
commit de43cc2e5c
8 changed files with 80 additions and 135 deletions

View File

@ -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<Tutorial> list = tutService.listTutorials();
model.addAttribute("tutorials", list);
return "index";
return "list";
}
public ITutorialsService getTutService() {
return tutService;
}
public void setTutService(ITutorialsService tutService) {
this.tutService = tutService;
}
}

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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;

View File

@ -1,19 +1,4 @@
<h1>Index</h1>
<h2>Tutorials list</h2>
<table border="1">
<tr>
<th>Tutorial Id</th>
<th>Tutorial Title</th>
<th>Tutorial Description</th>
<th>Tutorial Author</th>
</tr>
#foreach($tut in $tutorials)
<tr>
<td id="tutId_$foreach.count">$tut.tutId</td>
<td id="title_$foreach.count">$tut.title</td>
<td id="desc_$foreach.count">$tut.description</td>
<td id="auth_$foreach.count">$tut.author</td>
</tr>
#end
</table>
<h2>Welcome page</h2>
This is just the welcome page

View File

@ -0,0 +1,19 @@
<h1>Index</h1>
<h2>Tutorials list</h2>
<table border="1">
<tr>
<th>Tutorial Id</th>
<th>Tutorial Title</th>
<th>Tutorial Description</th>
<th>Tutorial Author</th>
</tr>
#foreach($tut in $tutorials)
<tr>
<td id="tutId_$foreach.count">$tut.tutId</td>
<td id="title_$foreach.count">$tut.title</td>
<td id="desc_$foreach.count">$tut.description</td>
<td id="auth_$foreach.count">$tut.author</td>
</tr>
#end
</table>

View File

@ -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));
}
}

View File

@ -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<Tutorial> tutorialListAttribute = (List<Tutorial>) model.asMap().get("tutorials");
assertEquals("index", view);
assertNotNull(tutorialListAttribute);
}
@Test public void testContent() throws Exception {
List<Tutorial> 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<Tutorial> createTutorialList() {
return Arrays.asList(new Tutorial(1, "TestAuthor", "Test Title", "Test Description"));
}
}