Test added for mvc - velocity (#510)
* dom4j * added more parsers * StaxParser * Jaxb binding * Jaxb binding * Finish article * apply some changes * Organize imports * remove dependency * Tutorial spring-mvc with velocity templates * velocity with spring mvc * tests
This commit is contained in:
parent
15abd8ef52
commit
1f5f1c6a48
1
pom.xml
1
pom.xml
|
@ -82,6 +82,7 @@
|
|||
<module>redis</module>
|
||||
|
||||
<module>mutation-testing</module>
|
||||
<module>spring-mvc-velocity</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,170 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<artifactId>spring-mvc-velocity</artifactId>
|
||||
|
||||
<name>spring-mvc-velocity</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity</artifactId>
|
||||
<version>1.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-tools</artifactId>
|
||||
<version>2.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-mvc-velocity</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<!-- <exclude>**/*ProductionTest.java</exclude> -->
|
||||
</excludes>
|
||||
<systemPropertyVariables>
|
||||
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>4.1.4.RELEASE</org.springframework.version>
|
||||
|
||||
|
||||
<!-- testing -->
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
<powermock.version>1.6.4</powermock.version>
|
||||
|
||||
<httpcore.version>4.4.1</httpcore.version>
|
||||
<httpclient.version>4.5</httpclient.version>
|
||||
|
||||
<rest-assured.version>2.4.1</rest-assured.version>
|
||||
|
||||
<!-- Maven plugins -->
|
||||
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.mvc.velocity.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
||||
import com.baeldung.mvc.velocity.service.TutorialsService;
|
||||
|
||||
@Controller
|
||||
public class MainController {
|
||||
|
||||
@Autowired
|
||||
private TutorialsService tutService;
|
||||
|
||||
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
|
||||
public String listTutorialsPage(Model model) {
|
||||
List<Tutorial> list = tutService.listTutorials();
|
||||
model.addAttribute("tutorials", list);
|
||||
return "index";
|
||||
}
|
||||
|
||||
public TutorialsService getTutService() {
|
||||
return tutService;
|
||||
}
|
||||
|
||||
public void setTutService(TutorialsService tutService) {
|
||||
this.tutService = tutService;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.mvc.velocity.domain;
|
||||
|
||||
public class Tutorial {
|
||||
|
||||
private Integer tutId;
|
||||
private String title;
|
||||
private String description;
|
||||
private String author;
|
||||
|
||||
public Tutorial() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Tutorial(Integer tutId, String title, String description, String author) {
|
||||
super();
|
||||
this.tutId = tutId;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public Integer getTutId() {
|
||||
return tutId;
|
||||
}
|
||||
|
||||
public void setTutId(Integer tutId) {
|
||||
this.tutId = tutId;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.mvc.velocity.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
||||
|
||||
@Service
|
||||
public class TutorialsService {
|
||||
|
||||
public List<Tutorial> listTutorials() {
|
||||
List<Tutorial> list = new ArrayList<Tutorial>();
|
||||
|
||||
list.add(new Tutorial(1, "Guava", "Introduction to Guava","GuavaAuthor"));
|
||||
list.add(new Tutorial(2, "Android", "Introduction to Android","AndroidAuthor"));
|
||||
return list;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
<div
|
||||
style="background: #63B175; text-align: center; padding: 5px; margin-top: 10px;">
|
||||
@Copyright baeldung.com
|
||||
</div>
|
|
@ -0,0 +1,5 @@
|
|||
<div style="background: #63B175; height: 80px; padding: 5px;">
|
||||
<div style="float: left">
|
||||
<h1>Our tutorials</h1>
|
||||
</div>
|
||||
</div>
|
|
@ -0,0 +1,22 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Spring & Velocity</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
#parse("/WEB-INF/fragments/header.vm")
|
||||
</div>
|
||||
|
||||
|
||||
<div>
|
||||
|
||||
<!-- View index.vm is inserted here -->
|
||||
$screen_content
|
||||
|
||||
</div>
|
||||
|
||||
<div>
|
||||
#parse("/WEB-INF/fragments/footer.vm")
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -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>
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<!-- Empty -->
|
||||
|
||||
</beans>
|
|
@ -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>$tut.tutId</td>
|
||||
<td>$tut.title</td>
|
||||
<td>$tut.description</td>
|
||||
<td>$tut.author</td>
|
||||
</tr>
|
||||
#end
|
||||
</table>
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
xsi:schemaLocation="
|
||||
http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
|
||||
|
||||
<display-name>Spring MVC Velocity</display-name>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>mvc</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/mvc-servlet.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>mvc</servlet-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/spring-context.xml</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
</web-app>
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.mvc.velocity.test;
|
||||
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.Model;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import com.baeldung.mvc.velocity.controller.MainController;
|
||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
||||
import com.baeldung.mvc.velocity.service.TutorialsService;
|
||||
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class NavigationControllerTest {
|
||||
|
||||
private MainController mainController;
|
||||
|
||||
private TutorialsService tutorialsService;
|
||||
|
||||
|
||||
private Model model;
|
||||
|
||||
@Before
|
||||
public final void setUp() throws Exception {
|
||||
model = new ExtendedModelMap();
|
||||
mainController = Mockito.spy(new MainController());
|
||||
tutorialsService = Mockito.mock(TutorialsService.class);
|
||||
|
||||
mainController.setTutService(tutorialsService);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void shouldGoToTutorialListView() {
|
||||
Mockito.when(tutorialsService.listTutorials()).thenReturn(TutorialDataFactory.createTutorialList());
|
||||
final String view = mainController.listTutorialsPage(model);
|
||||
final List<Tutorial> tutorialListAttribute = (List<Tutorial>) model.asMap().get("tutorials");
|
||||
|
||||
assertEquals("index", view);
|
||||
assertNotNull(tutorialListAttribute);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.mvc.velocity.test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
||||
|
||||
public final class TutorialDataFactory {
|
||||
|
||||
public static final Integer TEST_TUTORIAL_ID = 1;
|
||||
|
||||
public static final String TEST_TUTORIAL_AUTHOR = "TestAuthor";
|
||||
|
||||
public static final String TEST_TUTORIAL_TITLE = "Test Title";
|
||||
|
||||
public static final String TEST_TUTORIAL_DESCRIPTION = "Test Description";
|
||||
|
||||
|
||||
private TutorialDataFactory() {
|
||||
}
|
||||
|
||||
public static Tutorial createByDefault() {
|
||||
final Tutorial tutorial = new Tutorial();
|
||||
tutorial.setTutId(TEST_TUTORIAL_ID);
|
||||
tutorial.setAuthor(TEST_TUTORIAL_AUTHOR);
|
||||
tutorial.setTitle(TEST_TUTORIAL_TITLE);
|
||||
tutorial.setDescription(TEST_TUTORIAL_DESCRIPTION);
|
||||
return tutorial;
|
||||
}
|
||||
|
||||
public static Tutorial create(final Integer id , final String title) {
|
||||
final Tutorial tutorial = createByDefault();
|
||||
tutorial.setTutId(id);
|
||||
tutorial.setTitle(title);
|
||||
return tutorial;
|
||||
}
|
||||
|
||||
public static List<Tutorial> createTutorialList() {
|
||||
final List<Tutorial> tutorialList = new ArrayList<Tutorial>();
|
||||
tutorialList.add(createByDefault());
|
||||
return tutorialList;
|
||||
}
|
||||
|
||||
}
|
|
@ -27,12 +27,6 @@
|
|||
<version>2.0.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>xerces</groupId>
|
||||
<artifactId>xercesImpl</artifactId>
|
||||
<version>2.9.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<XMLTutorials>
|
||||
<tutorial tutId="01" type="xml">
|
||||
<title>XML with Dom4J</title>
|
||||
<description>XML handling with Dom4J</description>
|
||||
<date>14/06/2016</date>
|
||||
<author>Dom4J tech writer</author>
|
||||
</tutorial>
|
||||
</XMLTutorials>
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="XML">
|
||||
<author>Jaxb author</author>
|
||||
<date>04/02/2015</date>
|
||||
<description>XML Binding with Jaxb</description>
|
||||
<title>XML with Jaxb</title>
|
||||
</tutorial>
|
||||
</tutorials>
|
||||
|
|
Loading…
Reference in New Issue