Skip to content

Commit 0f6218e

Browse files
prashant1067pivovarit
authored andcommitted
adding spring controller changes (eugenp#493)
* adding spring controller changes * changing package name to baledung * removing test package * adding changes in pom for spring junit testing * adding test classes for controller * adding equals and hashcode for student for test cases * adding changes for running test cases
1 parent 5b3afb4 commit 0f6218e

10 files changed

Lines changed: 370 additions & 0 deletions

File tree

‎spring_controller/pom.xml‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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>test</groupId>
4+
<artifactId>test-mvc</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<packaging>war</packaging>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>org.springframework</groupId>
11+
<artifactId>spring-webmvc</artifactId>
12+
<version>4.3.0.RELEASE</version>
13+
14+
</dependency>
15+
<dependency>
16+
<groupId>javax.servlet</groupId>
17+
<artifactId>javax.servlet-api</artifactId>
18+
<version>3.0.1</version>
19+
<scope>compile</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.fasterxml.jackson.core</groupId>
23+
<artifactId>jackson-databind</artifactId>
24+
<version>2.6.3</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.fasterxml.jackson.core</groupId>
28+
<artifactId>jackson-annotations</artifactId>
29+
<version>2.6.3</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.fasterxml.jackson.core</groupId>
33+
<artifactId>jackson-core</artifactId>
34+
<version>2.6.3</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework</groupId>
38+
<artifactId>spring-web</artifactId>
39+
<version>4.3.0.RELEASE</version>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>junit</groupId>
44+
<artifactId>junit</artifactId>
45+
<version>4.12</version>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-test</artifactId>
51+
<version>4.1.7.RELEASE</version>
52+
</dependency>
53+
54+
</dependencies>
55+
56+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baledung.controller;
2+
3+
import org.springframework.web.bind.annotation.PathVariable;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
import com.baledung.student.Student;
8+
9+
@RestController
10+
public class RestAnnotatedController
11+
{
12+
@RequestMapping(value="/annotated/student/{studentId}")
13+
public Student getData(@PathVariable Integer studentId)
14+
{
15+
Student student = new Student();
16+
student.setName("Peter");
17+
student.setId(studentId);
18+
19+
return student;
20+
}
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baledung.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.ResponseBody;
8+
9+
import com.baledung.student.Student;
10+
11+
@Controller
12+
public class RestController{
13+
14+
/**
15+
* Get a student based on the id of the student
16+
* specified in path variable {studentId}
17+
* @param studentId
18+
* @return
19+
*/
20+
@RequestMapping(value="/student/{studentId}",method=RequestMethod.GET)
21+
public @ResponseBody Student getTestData(@PathVariable Integer studentId)
22+
{
23+
Student student = new Student();
24+
student.setName("Peter");
25+
student.setId(studentId);
26+
27+
return student;
28+
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
/**
3+
* @author Prashant Dutta
4+
*
5+
*/
6+
package com.baledung.controller;
7+
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.servlet.ModelAndView;
12+
13+
@Controller
14+
@RequestMapping(value="/test")
15+
public class TestController{
16+
17+
@RequestMapping(method=RequestMethod.GET)
18+
public ModelAndView getTestData()
19+
{
20+
ModelAndView mv = new ModelAndView();
21+
mv.setViewName("welcome");
22+
mv.getModel().put("data", "Welcome home man");
23+
24+
return mv;
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
*
3+
*/
4+
package com.baledung.student;
5+
6+
public class Student
7+
{
8+
private String name;
9+
10+
private int id;
11+
12+
//----------------------------- getters and setters--------------------------------------------------------
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public int getId() {
22+
return id;
23+
}
24+
25+
public void setId(int id) {
26+
this.id = id;
27+
}
28+
29+
@Override
30+
public int hashCode(){
31+
return this.id;
32+
}
33+
34+
@Override
35+
public boolean equals(Object obj){
36+
return this.name.equals(((Student)obj).getName());
37+
}
38+
39+
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:p="http://www.springframework.org/schema/p"
6+
xsi:schemaLocation="
7+
http://www.springframework.org/schema/beans
8+
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
9+
http://www.springframework.org/schema/context
10+
http://www.springframework.org/schema/context/spring-context-4.0.xsd
11+
http://www.springframework.org/schema/mvc
12+
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
13+
<context:component-scan base-package="com.baledung.controller" />
14+
<mvc:annotation-driven />
15+
16+
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17+
<property name="prefix">
18+
<value>/WEB-INF/</value>
19+
</property>
20+
<property name="suffix">
21+
<value>.jsp</value>
22+
</property>
23+
</bean>
24+
</beans>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:p="http://www.springframework.org/schema/p"
6+
xsi:schemaLocation="
7+
http://www.springframework.org/schema/beans
8+
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
9+
http://www.springframework.org/schema/context
10+
http://www.springframework.org/schema/context/spring-context-4.0.xsd
11+
http://www.springframework.org/schema/mvc
12+
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
13+
<context:component-scan base-package="com.test.controller" />
14+
<mvc:annotation-driven />
15+
16+
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17+
<property name="prefix">
18+
<value>/WEB-INF/</value>
19+
</property>
20+
<property name="suffix">
21+
<value>.jsp</value>
22+
</property>
23+
</bean>
24+
</beans>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
<web-app
4+
xmlns="http://java.sun.com/xml/ns/javaee"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
7+
version="3.0">
8+
9+
<servlet>
10+
<servlet-name>test-mvc</servlet-name>
11+
<servlet-class>
12+
org.springframework.web.servlet.DispatcherServlet
13+
</servlet-class>
14+
<load-on-startup>1</load-on-startup>
15+
<init-param>
16+
<param-name>contextConfigLocation</param-name>
17+
<param-value>/WEB-INF/test-mvc.xml</param-value>
18+
</init-param>
19+
</servlet>
20+
21+
<servlet-mapping>
22+
<servlet-name>test-mvc</servlet-name>
23+
<url-pattern>/test/*</url-pattern>
24+
</servlet-mapping>
25+
26+
27+
28+
29+
<welcome-file-list>
30+
<welcome-file>/WEB-INF/index.jsp</welcome-file>
31+
</welcome-file-list>
32+
</web-app>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>Insert title here</title>
8+
</head>
9+
<body>
10+
Data returned is ${data}
11+
</body>
12+
</html>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baledung.test;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.web.WebAppConfiguration;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
13+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14+
import org.springframework.web.context.WebApplicationContext;
15+
import org.springframework.web.servlet.ModelAndView;
16+
17+
import com.baledung.student.Student;
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
20+
/**
21+
* <p>
22+
* Test class for spring controllers
23+
* </p>
24+
* @author Prashant.Dutta
25+
*/
26+
@RunWith(SpringJUnit4ClassRunner.class)
27+
@WebAppConfiguration
28+
@ContextConfiguration({"classpath:test-mvc.xml"})
29+
public class ControllerTest
30+
{
31+
private MockMvc mockMvc;
32+
33+
@Autowired
34+
private WebApplicationContext wac;
35+
36+
private Student selectedStudent;
37+
38+
@Before
39+
public void setUp()
40+
{
41+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
42+
43+
selectedStudent = new Student();
44+
selectedStudent.setId(1);
45+
selectedStudent.setName("Peter");
46+
}
47+
48+
/**
49+
* Test basic test controller
50+
* @throws Exception
51+
*/
52+
@Test
53+
public void testTestController() throws Exception
54+
{
55+
56+
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/"))
57+
.andReturn()
58+
.getModelAndView();
59+
60+
// validate modal data
61+
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
62+
63+
// validate view name
64+
Assert.assertSame(mv.getViewName(), "welcome");
65+
}
66+
67+
/**
68+
* Test rest controller
69+
* @throws Exception
70+
*/
71+
@Test
72+
public void testRestController() throws Exception
73+
{
74+
75+
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/student/{studentId}",1))
76+
.andReturn().getResponse()
77+
.getContentAsString();
78+
79+
ObjectMapper reader = new ObjectMapper();
80+
81+
Student studentDetails = reader.readValue(responseBody, Student.class);
82+
83+
Assert.assertEquals(selectedStudent, studentDetails);
84+
85+
}
86+
87+
/**
88+
* Test rest controller annotated with test
89+
* @throws Exception
90+
*/
91+
@Test
92+
public void testRestAnnotatedController() throws Exception
93+
{
94+
95+
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/annotated/student/{studentId}",1))
96+
.andReturn().getResponse()
97+
.getContentAsString();
98+
99+
ObjectMapper reader = new ObjectMapper();
100+
101+
Student studentDetails = reader.readValue(responseBody, Student.class);
102+
103+
Assert.assertEquals(selectedStudent, studentDetails);
104+
}
105+
}

0 commit comments

Comments
 (0)