parent
53062a85df
commit
f4348cac35
|
@ -95,6 +95,12 @@
|
||||||
<version>${powermock.version}</version>
|
<version>${powermock.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
@RequestMapping("/")
|
||||||
public class MainController {
|
public class MainController {
|
||||||
|
|
||||||
private final TutorialsService tutService;
|
private final TutorialsService tutService;
|
||||||
|
|
|
@ -13,4 +13,22 @@ public class Tutorial {
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.author = author;
|
this.author = author;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getTutId() {
|
||||||
|
return tutId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class TutorialsService {
|
public class TutorialsService implements ITutorialsService {
|
||||||
|
|
||||||
public List<Tutorial> listTutorials() {
|
public List<Tutorial> listTutorials() {
|
||||||
return Arrays.asList(
|
return Arrays.asList(
|
||||||
|
|
|
@ -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"))
|
||||||
|
)
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,32 +1,41 @@
|
||||||
package com.baeldung.mvc.velocity.test;
|
package com.baeldung.mvc.velocity.test;
|
||||||
|
|
||||||
|
|
||||||
import com.baeldung.mvc.velocity.controller.MainController;
|
import static org.junit.Assert.assertEquals;
|
||||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import com.baeldung.mvc.velocity.service.TutorialsService;
|
import static org.mockito.Mockito.times;
|
||||||
import org.junit.Test;
|
import static org.mockito.Mockito.verify;
|
||||||
import org.junit.runner.RunWith;
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.mockito.runners.MockitoJUnitRunner;
|
|
||||||
import org.springframework.ui.ExtendedModelMap;
|
|
||||||
import org.springframework.ui.Model;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertNotNull;
|
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 {
|
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();
|
private final Model model = new ExtendedModelMap();
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void shouldGoToTutorialListView() {
|
public void shouldGoToTutorialListView() {
|
||||||
Mockito.when(mainController.getTutService().listTutorials())
|
Mockito.when(mainController.getTutService().listTutorials())
|
||||||
.thenReturn(createTutorialList());
|
.thenReturn(createTutorialList());
|
||||||
|
|
||||||
|
@ -37,6 +46,23 @@ public class NavigationControllerTest {
|
||||||
assertNotNull(tutorialListAttribute);
|
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() {
|
private static List<Tutorial> createTutorialList() {
|
||||||
return Arrays.asList(new Tutorial(1, "TestAuthor", "Test Title", "Test Description"));
|
return Arrays.asList(new Tutorial(1, "TestAuthor", "Test Title", "Test Description"));
|
||||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue