Test content (#515)

* Test Model Content

* Test get

* test get
This commit is contained in:
Raquel Garrido 2016-07-20 06:39:09 +02:00 committed by Grzegorz Piwowarek
parent 53062a85df
commit f4348cac35
8 changed files with 195 additions and 15 deletions

View File

@ -95,6 +95,12 @@
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
@RequestMapping("/")
public class MainController {
private final TutorialsService tutService;

View File

@ -13,4 +13,22 @@ public class Tutorial {
this.description = description;
this.author = author;
}
public Integer getTutId() {
return tutId;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getAuthor() {
return author;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.mvc.velocity.service;
import java.util.List;
import com.baeldung.mvc.velocity.domain.Tutorial;
public interface ITutorialsService {
public List<Tutorial> listTutorials();
}

View File

@ -7,7 +7,7 @@ import java.util.Arrays;
import java.util.List;
@Service
public class TutorialsService {
public class TutorialsService implements ITutorialsService {
public List<Tutorial> listTutorials() {
return Arrays.asList(

View File

@ -0,0 +1,81 @@
package com.baeldung.mvc.velocity.test;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.mvc.velocity.domain.Tutorial;
import com.baeldung.mvc.velocity.service.ITutorialsService;
import com.baeldung.mvc.velocity.service.TutorialsService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
@WebAppConfiguration
public class DataContentControllerTest {
private MockMvc mockMvc;
@Autowired
private ITutorialsService tutServiceMock;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
tutServiceMock = Mockito.mock(TutorialsService.class);
Mockito.reset(tutServiceMock);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testModel() 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")
));
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"))
)
)));
}
}

View File

@ -1,32 +1,41 @@
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 org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
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;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
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 com.baeldung.mvc.velocity.controller.MainController;
import com.baeldung.mvc.velocity.domain.Tutorial;
import com.baeldung.mvc.velocity.service.TutorialsService;
@RunWith(MockitoJUnitRunner.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
public class NavigationControllerTest {
private final MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
private MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
private final Model model = new ExtendedModelMap();
@Test
public final void shouldGoToTutorialListView() {
public void shouldGoToTutorialListView() {
Mockito.when(mainController.getTutService().listTutorials())
.thenReturn(createTutorialList());
@ -36,7 +45,24 @@ public class NavigationControllerTest {
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"));

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- Register the annotated components in the container eg : annotated controllers -->
<context:component-scan base-package="com.baeldung.mvc.velocity.*" />
<!-- Tell the container that we are going to use annotations -->
<context:annotation-config />
<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>/</value>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="layoutUrl" value="/WEB-INF/layouts/layout.vm" />
<property name="suffix" value=".vm" />
</bean>
</beans>