0% found this document useful (0 votes)
15 views25 pages

Workshop 11

Uploaded by

Surendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views25 pages

Workshop 11

Uploaded by

Surendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Unit Testing of controllers and transactions using

JUnit4/TestNG 6
Let us first create a CRUD application first as shown below and later We will learn how
to test Transaction and Controllers using either Junit4 and TestNG
1. Create a Dynamic Web Project named MVCCRUD –Right Click the Project and
Click on Configure-> Convert to Maven Project
2. Create a package named mvccrud.dao inside src folder and create Person.java
as shown below:

package mvccrud.model;
public class Person {

private int id;

private String name;

private String country;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getCountry() {


return country;
}

public void setCountry(String country) {


this.country = country;
}

@Override
public String toString(){
return "id="+id+", name="+name+", country="+country;
}
}
3. Create a package named mvccrud.dao inside src folder and create
PersonDAO.java interface and put following codes inside it:
package mvccrud.dao;
import java.util.List;
import mvccrud.model.Person;
public interface PersonDAO {

public void addPerson(Person p);


public void updatePerson(Person p);
public List<Person> listPersons();
public Person getPersonById(int id);
public void removePerson(int id);
}

4. Inside mvccrud.dao package, create a class named PersonDAOImp.java and put


following codes inside it:

package mvccrud.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import mvccrud.model.Person;

@Repository
public class PersonDAOImpl implements PersonDAO {

private JdbcTemplate jdbcTemplate;

public JdbcTemplate getJdbcTemplate() {


return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {


this.jdbcTemplate = jdbcTemplate;
}
public boolean isReadable()
{
return true;
}
public void addPerson(Person p) {
int id = p.getId();
String name = p.getName();
String country = p.getCountry();
int rows = jdbcTemplate.update("insert into person values(?,?,?)", id, name,
country);

//logger.info("Person saved successfully, Person Details="+p);


}

public void updatePerson(Person p) {


String query = "UPDATE person SET name=?,country=? WHERE
id=?";
jdbcTemplate.update(query,
new Object[] {
p.getName(),p.getCountry(), Integer.valueOf(p.getId())
});
}

public List<Person> listPersons() {

List<Person> personList = getJdbcTemplate().query("select * from


person",new BeanPropertyRowMapper(Person.class));
return personList;
}

public Person getPersonById(int id) {


String sql = "SELECT * FROM person WHERE id = ?";

Person person = (Person)getJdbcTemplate().queryForObject(


sql, new Object[] { id },
new BeanPropertyRowMapper(Person.class));

return person;
}

public void removePerson(int id) {


String query = "DELETE from person where id=?";
jdbcTemplate.update(query, new Object[] { Integer.valueOf(id) });
}

}
5. Create a package named mvccrud.service inside src folder and create
PersonService.java interface inside it as shown below:
package mvccrud.service;
import java.util.List;
import mvccrud.model.Person;
public interface PersonService {

public void addPerson(Person p);


public void updatePerson(Person p);
public List<Person> listPersons();
public Person getPersonById(int id);
public void removePerson(int id);

}
6. Inside mvccrud.service package create a class named PersonServiceImpl.java
and put following codes inside it:
package mvccrud.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import mvccrud.dao.PersonDAO;
import mvccrud.model.Person;

@Service
public class PersonServiceImpl implements PersonService {

@Autowired
private PersonDAO personDAO;

public void setPersonDAO(PersonDAO personDAO) {


this.personDAO = personDAO;
}
@Transactional
public void addPerson(Person p) {
this.personDAO.addPerson(p);
}

@Transactional
public void updatePerson(Person p) {
this.personDAO.updatePerson(p);
}

@Transactional
public List<Person> listPersons() {
return this.personDAO.listPersons();
}

@Transactional
public Person getPersonById(int id) {
return this.personDAO.getPersonById(id);
}

@Transactional
public void removePerson(int id) {
this.personDAO.removePerson(id);
}

7. Create a package named mvccrud.controller inside src folder, Create


PersonController.java inside it as shown below:
package mvccrud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import mvccrud.model.Person;
import mvccrud.service.PersonService;

@Controller
public class PersonController {

public PersonController()
{
System.out.println("Person Controller Called");
}
@RequestMapping("concat")
public String concat(@RequestParam String a, @RequestParam String b,
Model model) {
String result = a + b;
model.addAttribute("result", result);
return "concat";
}
private PersonService personService;

@Autowired(required=true)
@Qualifier(value="personService")
public void setPersonService(PersonService ps){
this.personService = ps;
}

@RequestMapping(value = "/persons", method = RequestMethod.GET)


public String listPersons(Model model) {
model.addAttribute("person", new Person());
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}

//For add and update person both


@RequestMapping(value= "/person/add", method =
RequestMethod.POST)
public String addPerson(@ModelAttribute("person") Person p){

System.out.println("Inside Add");
if(p.getId() == 0){
//new person, add it
this.personService.addPerson(p);
}else{
//existing person, call update
this.personService.updatePerson(p);
}

return "redirect:/persons";

@RequestMapping("/remove/{id}")
public String removePerson(@PathVariable("id") int id){

this.personService.removePerson(id);
return "redirect:/persons";
}

@RequestMapping("/edit/{id}")
public String editPerson(@PathVariable("id") int id, Model model){
model.addAttribute("person", this.personService.getPersonById(id));
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}

8. Add DispatcherServlet named “demo” to your project


9. Inside WebContent/WEB-INF directory, create applicationContext file named
“demo-servlet.xml” and put following codes inside it.

<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="mvccrud">
</context:component-scan>
<bean id="personService" class="mvccrud.service.PersonServiceImpl">
<property name="personDAO" ref="personDAO" />
</bean>
<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="personDAO" class="mvccrud.dao.PersonDAOImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="opmcm6718"/>
</bean>
</beans>

10. Make sure you have following codes inside pom.xml file

<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>SpringMVCCRUD</groupId>
<artifactId>SpringMVCCRUD</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>4.0.3.RELEASE</org.springframework-version>
<org.aspectj-version>1.7.4</org.aspectj-version>
<org.slf4j-version>1.7.5</org.slf4j-version>
<hibernate.version>4.0.1.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>

<!-- Hibernate -->

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>

<!-- Apache Commons DBCP -->


<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>

<!-- Logging -->


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.0.100-beta</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
11. Make sure you have following codes inside web.xml file

<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MVCCRUD</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>demo</display-name>
<servlet-name>demo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/demo</url-pattern>
<url-pattern>/</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

12. Create a table named person(id(int), name varchar(50), country varchar(50))


inside test database using phpmydin
13. Inside WEB-INF folder, create a folder named jsp and create person.jsp file
inside it as shown below:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page session="false" %>
<html>
<head>
<title>Person Page</title>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;border-color:#ccc;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px
5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-
color:#ccc;color:#333;background-color:#fff;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-
weight:normal;padding:10px 5px;border-style:solid;border-
width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-
color:#f0f0f0;}
.tg .tg-4eph{background-color:#f9f9f9}
</style>
</head>
<body>
<h1>
Add a Person
</h1>

<c:url var="addAction" value="/person/add" ></c:url>

<form:form action="${addAction}" commandName="person">


<table>
<c:if test="${!empty person.name}">
<tr>
<td>
<form:label path="id">
<spring:message text="ID"/>
</form:label>
</td>
<td>
<form:input path="id" readonly="true" size="8" disabled="true"
/>
<form:hidden path="id" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="name">
<spring:message text="Name"/>
</form:label>
</td>
<td>
<form:input path="name" />
</td>
</tr>
<tr>
<td>
<form:label path="country">
<spring:message text="Country"/>
</form:label>
</td>
<td>
<form:input path="country" />
</td>
</tr>
<tr>
<td colspan="2">
<c:if test="${!empty person.name}">
<input type="submit"
value="<spring:message text="Edit Person"/>" />
</c:if>
<c:if test="${empty person.name}">
<input type="submit"
value="<spring:message text="Add Person"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
<br>
<h3>Persons List</h3>
<c:if test="${!empty listPersons}">
<table class="tg">
<tr>
<th width="80">Person ID</th>
<th width="120">Person Name</th>
<th width="120">Person Country</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
<c:forEach items="${listPersons}" var="person">
<tr>
<td>${person.id}</td>
<td>${person.name}</td>
<td>${person.country}</td>
<td><a href="<c:url value='/edit/${person.id}' />" >Edit</a></td>
<td><a href="<c:url value='/remove/${person.id}' />"
>Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>

14. To Execute above application, Right Click Project-Run As-Run on Server-


Choose Tomcat and Run it.
We are going to develop unit test cases for above sample aaplication using Junit
and TestNG.Now, we are going to test transactions using Junit and TestNg.
Let us first test the transaction using TestNG. TestNG is also a testing framework like
Junit. TestNG is not installed on eclipse by default. Hence, to install TestNG, Click on
Help->Install New Software-> In Work With Field type http://beust.com/eclipse-> Press
Enter Key-> Select TestNG from below list -> Click on Next Button
Create a package named “unittestcase” inside src folder and create a Java file named
TranTestNG.java inside it as shown below:

package unittestcase;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.After;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.Test;
import junit.framework.Assert;
import mvccrud.dao.PersonDAOImpl;
import mvccrud.model.Person;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"demo-servlet.xml"})
@WebAppConfiguration
public class TranTestNg extends AbstractTransactionalTestNGSpringContextTests{
@Autowired
PersonDAOImpl personDAO;
@After
@Test
public void testPhoneLogIsReadable() {
System.out.println("Inside testPhoneLogReadable");
assertTrue("Phone log is not readable.", personDAO.isReadable());
List<Person> lst=personDAO.listPersons();
for(int i=0;i<lst.size();i++)
{
System.out.println(lst.get(i).getId()+"-"+ lst.get(i).getName()
+"-"+lst.get(i).getCountry());
}
}
@Test
@Rollback
@Transactional
public void testAddition()
{
System.out.println("Inside TestAddition");
Person p=new Person();
p.setId(14);
p.setName("Pitu");
p.setCountry("LANKA");
personDAO.addPerson(p);
Person p1=personDAO.getPersonById(14);
Assert.assertNotNull(p1.getId());

}
}

In above example, “testAddition” method tries to record a detail with id=14, but since
this is for testing purpose only, the record is actually not stored on the database. Upper
method testPhoneLogIsReadable() is executed after testAddition( ) and it does not
display record with id=14
To execute above program, Right Click the File->Run As->TestNG Test
Now, let us test the transaction using Junit
Create a class named TranTestJUnit inside unittestcase package as shown below:
package unittestcase;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;

import junit.framework.Assert;
import mvccrud.dao.PersonDAOImpl;
import mvccrud.model.Person;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"demo-servlet.xml"})
@WebAppConfiguration
public class TranTestJUnit {
@Autowired
PersonDAOImpl personDAO;
@After

public void testPhoneLogIsReadable() {


assertTrue("Phone log is not readable.", personDAO.isReadable());
List<Person> lst=personDAO.listPersons();
for(int i=0;i<lst.size();i++)
{
System.out.println(lst.get(i).getId()+"-"+ lst.get(i).getName()
+"-"+lst.get(i).getCountry());
}
}
@Test
@Rollback
@Transactional
public void testAddition()
{
Person p=new Person();
p.setId(15);
p.setName("Pitu");
p.setCountry("LANKA");
personDAO.addPerson(p);
Person p1=personDAO.getPersonById(15);
Assert.assertNotNull(p1.getId());
}
}

In above code as well record with id=15 is not really stored on database, which is
verified by upper method execution output. However, during testing record with id=15
seems stored on database as Assert.assertNotNull ( ) will return true.

Unit Testing of Controller


Inside PersonController class, create following method:

@RequestMapping("concat")
public String concat(@RequestParam String a, @RequestParam String b, Model
model) {
String result = a + b;
model.addAttribute("result", result);
return "concat";
}

Create concat.jsp file inside WEB-INF/jsp folder and keep following codes inside it:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${result}
</body>
</html>

Now, we are going to develop a Junit test case that will issue http GET request(/contact)
on above method. Right click on unittestcase package-> New->Other->Select Junit Test
Case-> Create a test case named TestControllerMethod.java and put following codes
inside it:
package unittestcase;

import org.junit.Test;
import org.junit.runner.RunWith;
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.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import org.junit.*;

import junit.framework.Assert;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;


import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"demo-servlet.xml"})
@WebAppConfiguration
public class TestControllerMethod {

@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test()

public void testTest1() throws Exception {


/*this.mockMvc =

MockMvcBuilders.webAppContextSetup(this.wac).build();
Assert.assertNotNull(mockMvc);*/
this.mockMvc.perform(post("/concat").param("a",
"red").param("b", "apple"))
.andExpect(status().isOk())
.andExpect(model().attribute("result", "redapple"))
.andExpect(forwardedUrl("/WEB-INF/jsp/concat.jsp"))
.andDo(MockMvcResultHandlers.print());

}
}

Above code will send “red” and “apple” as parameteran the “concat” method inside
TestController class. The method concatenates the two string and will return back to the
client.
To Execute above program, Right Click on it->Run As-> Junit Test
You will see following output on the console
MockHttpServletRequest:
HTTP Method = POST
Request URI = /concat
Parameters = {a=[red], b=[apple]}
Headers = {}
Handler:
Type = mvccrud.controller.PersonController
Async:
Was async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = concat
View = null
Attribute = result
value = redapple
FlashMap:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = /WEB-INF/jsp/concat.jsp
Redirected URL = null
Cookies = []

Note: D:\springtest is the project folder for above application.

Mocks vs Stubs
If you want to test a method that uses an object of another class, then in order to test
such method there exists two different approaches.
1. Create Mock object of Dependent class and test that method
2. Create Stub object of Dependent class and test that method
Mocking
1. Inside unittest package, create a class named ClassOne.java and put following
codes inside it.
package unittest;

public class ClassOne {

public String oneMessage()


{
return "We are United ";
}
}

2. In the same class, create another class named ClassTwo.java and put the
following codes inside it.
package unittest;
public class ClassTwo {

private final ClassOne one;


public ClassTwo(ClassOne one)
{
this.one=one;
}
public String twoMessage()
{
String msg=one.oneMessage();
return msg;
}

}
If we want to test “twoMessage( )” method as shown above, we have to know
that this particular method is dependent on another object named “one” that
belongs to ClassOne.java class.

3. In order to Test “twoMessage” method of ClassTwo class, create a Junit Test


Case named MockTest.java inside “unittestcase” package and put following
codes inside it:
package unittestcase;
import static org.junit.Assert.*;
import org.junit.Test;
import org.mockito.Mockito;
import unittest.ClassOne;
import unittest.ClassTwo;
public class MockTest {
@Test
public void testtwoMessage() {

1. ClassOne mockOne=Mockito.mock(ClassOne.class);
2. Mockito.when(mockOne.oneMessage()).thenReturn("ok");
3. ClassTwo two =new ClassTwo(one);
4. assertEquals("ok",two.twoMessage());
}

In above code, (line 1)we have not created actual object of ClassOne object rather
mock object of ClassOne named “mockOne” and passed it to ClasstTwo in line no
3. Line No 2, will cause oneMessage( ) method to return always “ok” when get
called. LineNo 4, performs unit testing of “twoMessage” method.
To Run above program. Right Click on TestMock->Run As-> Junit Test and will
successfully be executed
Stub/ Stubbing Beans While Testing
In above example, we have created mock object of ClassOne and passed it to the
ClassTwo object since the method to be tested( i.e. “twoMessage” of ClassTwo)
uses or is dependent on ClassOne object.
Create a class named StubTest inside unittestcase package and put following codes
inside it.
package unittestcase;

import static org.junit.Assert.*;


import org.junit.Before;
import org.junit.Test;
import unittest.ClassOne;
import unittest.ClassOneStub;
import unittest.ClassTwo;

public class StubTest {

private ClassOne stub;


@Test
public void testtwoMessage() {
ClassOneStub stub=new ClassOneStub();
ClassTwo two=new ClassTwo(stub);
assertEquals("ok",two.twoMessage());

}
In above example, we have passed stub object to ClassTwo instead of mock.
ClassOneStub does not fully incorporate exact source code of ClassOne, rather only
minimal required code only.
ClassOneStub is kept inside unittest package has following code:
package unittest;

public class ClassOneStub implements ClassOneInterface {

public String oneMessage() {


return "ok";
}

ClassOneInterface is also inside unittest package and has following codes:


package unittest;

public interface ClassOneInterface {


public String oneMessage();
}
Modify ClassTwo.java as following
package unittest;

public class ClassTwo {

private final ClassOneInterface one;


public ClassTwo(ClassOneInterface one)
{
this.one=one;
}
public String twoMessage()
{
String msg=one.oneMessage();
return msg;
}

ClassOne.java uses the same coding as shown above.


Acceptance Testing
Unit tests only cover subset of different interactions between components of
application. Acceptance tests, on the other hand boot up the complete application
and allows us to interact with its interface. Selenium Framework is the most popular
for designing effective acceptance test cases.
Application Deployment
We can deploy our web application to Cloud on Cloud Foundry backed by Pivotal
Web Service or Heroku
1. Create account by logging into the following link:
https://account.run.pivotal.io/sign-up
2. Install CF CLI by logging into your account and Clicking on Tools link on
Dashboard. You have to download CFCLI App in order to install it. After
installation, type cf help to verify whether CF CLI is installed successfully in your
machine.
3. We are going to deploy MVCCRUD application that we had created earlier. In
order to deploy it, you should have war file of this app. To get war file, Right Click
MVCCRUD-> Run As->Run Configuration-> Enter the information as shown
below:

Click on Run Button. War file will be created that we will keep on PWS(Pivotal
Web Service)
4. Open Command prompt and type following command to logon to PWS
cf login -a api.run.pivotal.io -u [email protected] -p
opmcm6718 -o opmcm
5. Go to target folder of your MVCCRUD application and type following command to
push or deploy MVCCRUD application to PWS.

cf push MVCCRUD -p SpringMVCCRUD-0.0.1-SNAPSHOT.war

6. Click on URL mentioned on Route column of your App as shown below, your
APP will be executed.
Deploying Spring Web Application on Heroku
heroku plugins:install https://github.com/heroku/heroku-deploy

1. Create an account on Heroku by logging into following URL:

https://signup.heroku.com/dc
2. Login to heoku from your created account
3. Click on NewApp and give your App a new name “mvccrud”
4. Download and install toolblet from following URL:

https://devcenter.heroku.com/articles/getting-started-with-java#set-up
5. Create war file of the mvccrud application as shown previously for pivotal web
service application
6. Open Command prompt and type heroku login and provide your email
address and password
7. In the same command prompt type the following URL:

heroku plugins:install https://github.com/heroku/heroku-deploy

8. Type the following URL to deploy your war file


heroku deploy:war --war SpringMVCCRUD-0.0.1-SNAPSHOT.war --app
mvccrud
9. Logon to heroku->Click on Personal Apps->mvccrud-> Click on OpenAPp
Button-> Your app starts executing.

10. asdsad

You might also like