`
sillycat
  • 浏览: 2542630 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring3 and REST Integeration(VII)Controller JUnit Test and Mock/Spring Test MVC

 
阅读更多
Spring3 and REST Integeration(VII)Controller JUnit Test and Mock/Spring Test MVC
Spring Test MVC
I found a spring mvc test framework in github.com, that is nice to have this open source project.

The pom.xml will be changed as follow to get dependencies:
<repositories]]>
<!-- http://maven.springframework.org/snapshot -->
<repository]]>
            <id]]>springframe_snapshot_repo</id]]>
            <name]]>springframe snapshot repo</name]]>
            <url]]>http://maven.springframework.org/snapshot</url]]>
        </repository]]>
<!-- http://repository.sonatype.org/content/groups/public/ -->
<repository]]>
            <id]]>sonatype_repo</id]]>
            <name]]>sonatyperepo</name]]>
            <url]]>http://repository.sonatype.org/content/groups/public/</url]]>
        </repository]]>
<!--  http://developer.k-int.com/maven2/ -->
<repository]]>
            <id]]>k_int_repo</id]]>
            <name]]>k intrepo</name]]>
            <url]]>http://developer.k-int.com/maven2/</url]]>
        </repository]]>
<repository]]>
            <id]]>maven_repo1</id]]>
            <name]]>maven repo1 Repository</name]]>
            <url]]>http://search.maven.org</url]]>
        </repository]]>
        <repository]]>
        <id]]>maven_jboss</id]]>
        <name]]>mavenjboss repository</name]]>
        <url]]>http://repository.jboss.com</url]]>
        </repository]]>
        <repository]]>
        <id]]>maven_java</id]]>
        <name]]>maven java repository</name]]>
        <url]]>http://download.java.net/maven/2/</url]]>
        </repository]]>
    <repository]]>
            <id]]>sillycat</id]]>
            <name]]>sillycat Repository</name]]>
            <url]]>https://sillycat.googlecode.com/svn/repository</url]]>
        </repository]]>
        <repository]]>
            <id]]>alfresco-maven-release</id]]>
            <name]]>Alfresco Maven Release Repository</name]]>
            <url]]>http://maven.alfresco.com/nexus/content/repositories/releases/</url]]>
        </repository]]>
</repositories]]>

<pluginRepositories]]>
<pluginRepository]]>
            <id]]>springframe_snapshot_repo</id]]>
            <name]]>springframe snapshot repo</name]]>
            <url]]>http://maven.springframework.org/snapshot</url]]>
        </pluginRepository]]>
<pluginRepository]]>
<id]]>sonatype_repo</id]]>
            <name]]>sonatyperepo</name]]>
            <url]]>http://repository.sonatype.org/content/groups/public/</url]]>
        </pluginRepository]]>
<pluginRepository]]>
            <id]]>k_int_repo</id]]>
            <name]]>k intrepo</name]]>
            <url]]>http://developer.k-int.com/maven2/</url]]>
        </pluginRepository]]>
<pluginRepository]]>
            <id]]>maven_repo1</id]]>
            <name]]>maven repo1 Repository</name]]>
            <url]]>http://search.maven.org</url]]>
        </pluginRepository]]>
        <pluginRepository]]>
        <id]]>maven_jboss</id]]>
        <name]]>mavenjboss repository</name]]>
        <url]]>http://repository.jboss.com</url]]>
        </pluginRepository]]>
        <pluginRepository]]>
        <id]]>maven_java</id]]>
        <name]]>maven java repository</name]]>
        <url]]>http://download.java.net/maven/2/</url]]>
        </pluginRepository]]>
    <pluginRepository]]>
            <id]]>sillycat</id]]>
            <name]]>sillycat Repository</name]]>
            <url]]>https://sillycat.googlecode.com/svn/repository</url]]>
        </pluginRepository]]>
        <pluginRepository]]>
            <id]]>alfresco-maven-release</id]]>
            <name]]>Alfresco Maven Release Repository</name]]>
            <url]]>http://maven.alfresco.com/nexus/content/repositories/releases/</url]]>
        </pluginRepository]]>
</pluginRepositories]]>


<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-context</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-web</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-webmvc</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
</dependency]]>
<dependency]]>
<groupId]]>javax.servlet</groupId]]>
<artifactId]]>servlet-api</artifactId]]>
<version]]>3.0-alpha-1</version]]>
<scope]]>provided</scope]]>
</dependency]]>
<dependency]]>
<groupId]]>org.codehaus.jackson</groupId]]>
<artifactId]]>jackson-core-asl</artifactId]]>
<version]]>1.9.6</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.codehaus.jackson</groupId]]>
<artifactId]]>jackson-mapper-asl</artifactId]]>
<version]]>1.9.6</version]]>
</dependency]]>
<dependency]]>
<groupId]]>log4j</groupId]]>
<artifactId]]>log4j</artifactId]]>
<version]]>1.2.15</version]]>
</dependency]]>
<dependency]]>
<groupId]]>junit</groupId]]>
<artifactId]]>junit</artifactId]]>
<version]]>4.10</version]]>
<scope]]>test</scope]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-test</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
<scope]]>test</scope]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-mock</artifactId]]>
<version]]>2.0.8</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.mockito</groupId]]>
<artifactId]]>mockito-core</artifactId]]>
<version]]>1.9.0</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-test-mvc</artifactId]]>
<version]]>1.0.0.BUILD-SNAPSHOT</version]]>
</dependency]]>
<dependency]]>
<groupId]]>com.jayway.jsonpath</groupId]]>
<artifactId]]>json-path</artifactId]]>
<version]]>0.5.6</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.hamcrest</groupId]]>
<artifactId]]>hamcrest-core</artifactId]]>
<version]]>1.3.RC2</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.hamcrest</groupId]]>
<artifactId]]>hamcrest-library</artifactId]]>
<version]]>1.3.RC2</version]]>
</dependency]]>

And the test implementation will be as follow:
package com.sillycat.easyrestserver.controller;

import static org.springframework.test.web.server.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.status;

import java.io.IOException;

import javax.servlet.ServletException;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.server.request.MockMvcRequestBuilders;
import org.springframework.test.web.server.setup.MockMvcBuilders;

import com.sillycat.easyrestserver.model.Company;
import com.sillycat.easyrestserver.model.Person;
import com.sillycat.easyrestserver.service.PersonService;

public class PersonControllerTest {

     @Mock
     private PersonService mockPersonService;

     PersonController personController;

     Person person;

     ObjectMapper jsonMapper;

     @Before
     public void setUp() throws ServletException, IOException {
          MockitoAnnotations.initMocks(this);

          personController = new PersonController();
          personController.setPersonService(mockPersonService);

          person = new Person();
          person.setCompany(new Company());
          person.setId(3);
          person.setPersonName("person3");

          jsonMapper = new ObjectMapper();
     }

     @Test
     public void get() throws Exception {

          Mockito.when(mockPersonService.get(3)).thenReturn(person);

          MockMvcBuilders
                    .standaloneSetup(personController)
                    .build()
                    .perform(
                              MockMvcRequestBuilders.get("/person/3").accept(
                                        MediaType.APPLICATION_JSON))
                    .andExpect(status().isOk())
                    .andExpect(content().type(MediaType.APPLICATION_JSON))
                    .andExpect(jsonPath("$.personName").value("person3"));
     }

     @Test
     public void add() throws Exception {
          person.setId(null);
          String jsonPerson = jsonMapper.writeValueAsString(person);
          MockMvcBuilders
                    .standaloneSetup(personController)
                    .build()
                    .perform(
                              MockMvcRequestBuilders.post("/person")
                                        .contentType(MediaType.APPLICATION_JSON)
                                        .accept(MediaType.APPLICATION_JSON)
                                        .body(jsonPerson.getBytes()))
                    .andExpect(status().isOk())
                    .andExpect(content().type(MediaType.APPLICATION_JSON))
                    .andExpect(jsonPath("$.personName").value("person3"));
     }
}


references:

分享到:
评论

相关推荐

    Pro Spring for Integeration

    ### Pro Spring for Integration #### 一、概览与背景 《Pro Spring for Integration》是一本由著名出版社Apress出版的英文书籍,旨在介绍如何利用Spring Integration框架构建企业级的集成解决方案。Spring作为一...

    Spring面试题 75道.pdf

    而这些组件被分别整合在核心容器(Core Container)、AOP(Aspect Oriented Programming)和设备支持(Instrmentation)、数据访问与集成(Data Access/Integeration)、Web、消息(Messaging)和Test等6个模块中。

    工作流人工任务 Websphere Integeration Development 开发

    在WebSphere Integration Development中,可以使用Business Process Model and Notation (BPMN) 图形标准来描绘这些流程。 然后,我们需要创建人工任务节点。这些任务可以是简单的信息输入,也可以是复杂的决策过程...

    docker-jenkins-integeration

    docker-jenkins-integeration

    Process Integration7.0标准的安装文档

    标题:Process Integration 7.0标准的安装文档 在IT领域,尤其是企业级软件的部署与维护中,标准安装文档扮演着至关重要的角色。本文档是关于SAP NetWeaver Process Integration (PI) 7.1版本在Windows操作系统上...

    西门子SCL语言实现低阶及高阶积分算法仿真案例.docx

    三阶方程y=ax^3+bx^2+cx+d积分则得到y=ax^4/4+bx^3/3+cx^2/2+dx+m,其中m均为积分常数。 在SCL编程中实现积分算法,通常需要将X轴分成多个小段(长条),每一段近似为梯形,然后计算每个梯形的面积并累加。梯形面积...

    kettle下载-一款免费开源ETL工具

    3. **Steps**:Kettle提供了丰富的预定义步骤,涵盖数据输入、输出、转换等各个方面。用户也可以自定义步骤以满足特定需求。例如,`Table Input`用于从数据库中读取数据,`CSV File Output`则用于写入CSV文件。 4. ...

    ETL之PDI/Kettle9.x/8.x案例培训实战从入门到应用案例

    利用碎片化的时间学习即可,每天学习一小时或3个课时即可达到效果;时间充足者,自由安排 课程目标: 促使教育公平,打破教育壁垒,让ETL更加的大众化使用 课程简介: 【可视化介绍】 Kettle 是 PDI 以前的名称...

    商务智能应用实例.pptx

    2. 新建一个Integeration Service项目 3. 在该项目中,右键单击SSIS包,然后选择“新建SSIS包” 10.2 商务智能中的数据挖掘模型设计 数据挖掘模型设计是指根据商务智能应用的需求,设计和建立数据挖掘模型,以便...

    bootstrap-easy-sidebar, 围绕主题集成和灵活性设计的Bootstrap 工具条.zip

    bootstrap-easy-sidebar, 围绕主题集成和灵活性设计的Bootstrap 工具条 bootstrap-easy-sidebar我看到了 Bootstrap的几个边栏,找到了大多数缺乏主题integeration和灵活性的领域。 通过继承导航栏的所有样式,然后在...

    i4C_Industry_4_0_Cell

    工业 4.0 单元 (i4C) - 根据工业 4.0 概念的机器人单元该项目的第一阶段涉及工业 4.0 背景下的机器人单元设计,并在布尔诺理工大学机械工程学院自动化与计算机科学研究所实施。 这一阶段项目的主要部分是单元的建设...

Global site tag (gtag.js) - Google Analytics