Skip to content

Commit 6fb2305

Browse files
Sunil GulabaniSunil Gulabani
authored andcommitted
Integration Testing with Spring MVC.
1 parent 7c34948 commit 6fb2305

12 files changed

Lines changed: 577 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<module>mutation-testing</module>
108108
<module>spring-mvc-velocity</module>
109109
<module>xstream</module>
110+
<module>spring-mvc-test</module>
110111
</modules>
111112

112113
</project>

‎spring-mvc-test/README‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
To compile and run the project, execute following command:
2+
3+
mvn clean install org.codehaus.cargo:cargo-maven2-plugin:run
4+
5+
URL: http://localhost:8080/spring-mvc-test/

‎spring-mvc-test/pom.xml‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.baeldung</groupId>
4+
<artifactId>spring-mvc-test</artifactId>
5+
<version>0.1-SNAPSHOT</version>
6+
<name>spring-mvc-test</name>
7+
<packaging>war</packaging>
8+
9+
<properties>
10+
<!-- Spring -->
11+
<org.springframework.version>4.3.1.RELEASE</org.springframework.version>
12+
<!-- logging -->
13+
<org.slf4j.version>1.7.21</org.slf4j.version>
14+
<logback.version>1.1.7</logback.version>
15+
<!-- util -->
16+
<commons-lang3.version>3.4</commons-lang3.version>
17+
<!-- testing -->
18+
<org.hamcrest.version>1.3</org.hamcrest.version>
19+
<junit.version>4.12</junit.version>
20+
<mockito.version>1.10.19</mockito.version>
21+
<httpcore.version>4.4.5</httpcore.version>
22+
<httpclient.version>4.5.2</httpclient.version>
23+
24+
<!-- maven plugins -->
25+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
26+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
27+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
28+
<maven-failsafe-plugin.version>2.19.1</maven-failsafe-plugin.version>
29+
<maven-resources-plugin.version>3.0.1</maven-resources-plugin.version>
30+
31+
<!-- server -->
32+
<cargo-maven2-plugin.version>1.5.0</cargo-maven2-plugin.version>
33+
34+
</properties>
35+
36+
<dependencies>
37+
<!-- Spring -->
38+
<dependency>
39+
<groupId>org.springframework</groupId>
40+
<artifactId>spring-web</artifactId>
41+
<version>${org.springframework.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework</groupId>
45+
<artifactId>spring-webmvc</artifactId>
46+
<version>${org.springframework.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-websocket</artifactId>
51+
<version>${org.springframework.version}</version>
52+
</dependency>
53+
<!-- Jackson -->
54+
<dependency>
55+
<groupId>com.fasterxml.jackson.core</groupId>
56+
<artifactId>jackson-core</artifactId>
57+
<version>2.7.3</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.fasterxml.jackson.core</groupId>
61+
<artifactId>jackson-databind</artifactId>
62+
<version>2.7.3</version>
63+
</dependency>
64+
<!-- web -->
65+
<dependency>
66+
<groupId>javax.servlet</groupId>
67+
<artifactId>javax.servlet-api</artifactId>
68+
<version>3.0.1</version>
69+
<scope>provided</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>javax.servlet</groupId>
73+
<artifactId>jstl</artifactId>
74+
<version>1.2</version>
75+
<scope>runtime</scope>
76+
</dependency>
77+
<!-- logging -->
78+
<dependency>
79+
<groupId>org.slf4j</groupId>
80+
<artifactId>slf4j-api</artifactId>
81+
<version>${org.slf4j.version}</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.slf4j</groupId>
85+
<artifactId>slf4j-log4j12</artifactId>
86+
<version>${org.slf4j.version}</version>
87+
</dependency>
88+
<!-- test scoped -->
89+
<dependency>
90+
<groupId>junit</groupId>
91+
<artifactId>junit</artifactId>
92+
<version>${junit.version}</version>
93+
<scope>test</scope>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.mockito</groupId>
97+
<artifactId>mockito-core</artifactId>
98+
<version>${mockito.version}</version>
99+
<scope>test</scope>
100+
</dependency>
101+
<dependency>
102+
<groupId>com.jayway.jsonpath</groupId>
103+
<artifactId>json-path</artifactId>
104+
<version>2.2.0</version>
105+
</dependency>
106+
<dependency>
107+
<groupId>org.springframework</groupId>
108+
<artifactId>spring-test</artifactId>
109+
<version>${org.springframework.version}</version>
110+
<scope>test</scope>
111+
</dependency>
112+
</dependencies>
113+
114+
<build>
115+
<finalName>spring-mvc-test</finalName>
116+
<resources>
117+
<resource>
118+
<directory>src/main/resources</directory>
119+
<filtering>true</filtering>
120+
</resource>
121+
</resources>
122+
123+
<plugins>
124+
<plugin>
125+
<groupId>org.apache.maven.plugins</groupId>
126+
<artifactId>maven-compiler-plugin</artifactId>
127+
<version>${maven-compiler-plugin.version}</version>
128+
<configuration>
129+
<source>1.8</source>
130+
<target>1.8</target>
131+
</configuration>
132+
</plugin>
133+
134+
<plugin>
135+
<artifactId>maven-resources-plugin</artifactId>
136+
<version>2.7</version>
137+
</plugin>
138+
139+
<plugin>
140+
<groupId>org.apache.maven.plugins</groupId>
141+
<artifactId>maven-war-plugin</artifactId>
142+
<version>${maven-war-plugin.version}</version>
143+
<configuration>
144+
<failOnMissingWebXml>false</failOnMissingWebXml>
145+
</configuration>
146+
</plugin>
147+
<plugin>
148+
<groupId>org.apache.maven.plugins</groupId>
149+
<artifactId>maven-surefire-plugin</artifactId>
150+
<version>${maven-surefire-plugin.version}</version>
151+
<configuration>
152+
<excludes>
153+
<exclude>**/*IntegrationTest.java</exclude>
154+
</excludes>
155+
</configuration>
156+
</plugin>
157+
<plugin>
158+
<groupId>org.apache.maven.plugins</groupId>
159+
<artifactId>maven-failsafe-plugin</artifactId>
160+
<version>${maven-failsafe-plugin.version}</version>
161+
<configuration>
162+
<includes>
163+
<include>**/*IntegrationTest.java</include>
164+
</includes>
165+
</configuration>
166+
<executions>
167+
<execution>
168+
<goals>
169+
<goal>integration-test</goal>
170+
<goal>verify</goal>
171+
</goals>
172+
</execution>
173+
</executions>
174+
</plugin>
175+
<plugin>
176+
<groupId>org.codehaus.cargo</groupId>
177+
<artifactId>cargo-maven2-plugin</artifactId>
178+
<version>${cargo-maven2-plugin.version}</version>
179+
<configuration>
180+
<container>
181+
<containerId>jetty8x</containerId>
182+
<type>embedded</type>
183+
</container>
184+
<configuration>
185+
<properties>
186+
<cargo.servlet.port>8080</cargo.servlet.port>
187+
</properties>
188+
</configuration>
189+
</configuration>
190+
</plugin>
191+
</plugins>
192+
</build>
193+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
11+
import org.springframework.web.servlet.view.JstlView;
12+
13+
@EnableWebMvc
14+
@Configuration
15+
@ComponentScan(basePackages = {"com.baeldung.spring.controller"})
16+
public class ApplicationConfig extends WebMvcConfigurerAdapter {
17+
18+
public ApplicationConfig() {
19+
super();
20+
}
21+
22+
@Override
23+
public void addViewControllers(final ViewControllerRegistry registry) {
24+
super.addViewControllers(registry);
25+
registry.addViewController("/").setViewName("index");
26+
}
27+
28+
@Bean
29+
public ViewResolver viewResolver() {
30+
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
31+
bean.setViewClass(JstlView.class);
32+
bean.setPrefix("/WEB-INF/jsp/");
33+
bean.setSuffix(".jsp");
34+
return bean;
35+
}
36+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.spring.bean;
2+
3+
public class Greeting {
4+
private int id;
5+
private String message;
6+
7+
public int getId() {
8+
return id;
9+
}
10+
public void setId(int id) {
11+
this.id = id;
12+
}
13+
public String getMessage() {
14+
return message;
15+
}
16+
public void setMessage(String message) {
17+
this.message = message;
18+
}
19+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.spring.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.ResponseBody;
9+
10+
import com.baeldung.spring.bean.Greeting;
11+
12+
@Controller
13+
public class GreetController {
14+
15+
@RequestMapping(value = "/homePage", method = RequestMethod.GET)
16+
public String index() {
17+
return "index";
18+
}
19+
20+
@RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json")
21+
@ResponseBody
22+
public Greeting greet() {
23+
Greeting greeting = new Greeting();
24+
greeting.setId(1);
25+
greeting.setMessage("Hello World!!!");
26+
return greeting;
27+
}
28+
29+
@RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json")
30+
@ResponseBody
31+
public Greeting greetWithPathVariable(@PathVariable("name") String name) {
32+
Greeting greeting = new Greeting();
33+
greeting.setId(1);
34+
greeting.setMessage("Hello World " + name + "!!!");
35+
return greeting;
36+
}
37+
38+
@RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json")
39+
@ResponseBody
40+
public Greeting greetWithQueryVariable(@RequestParam("name") String name) {
41+
Greeting greeting = new Greeting();
42+
greeting.setId(1);
43+
greeting.setMessage("Hello World " + name + "!!!");
44+
return greeting;
45+
}
46+
47+
@RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json")
48+
@ResponseBody
49+
public Greeting greetWithPost() {
50+
Greeting greeting = new Greeting();
51+
greeting.setId(1);
52+
greeting.setMessage("Hello World!!!");
53+
return greeting;
54+
}
55+
56+
@RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json")
57+
@ResponseBody
58+
public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) {
59+
Greeting greeting = new Greeting();
60+
greeting.setId(id);
61+
greeting.setMessage("Hello World " + name + "!!!");
62+
return greeting;
63+
}
64+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
5+
</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<logger name="org.springframework" level="WARN" />
10+
<logger name="org.springframework.transaction" level="WARN" />
11+
12+
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
13+
<logger name="org.springframework.web.servlet.mvc" level="TRACE" />
14+
15+
<root level="INFO">
16+
<appender-ref ref="STDOUT" />
17+
</root>
18+
</configuration>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h1>Spring MVC - Integration Testing</h1>
4+
</body>
5+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd" >
5+
</beans>

0 commit comments

Comments
 (0)