spring mvc 单元测试示例

import java.awt.print.Printable;
import java.io.IOException;  

import javax.servlet.http.HttpServletResponse;  

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.ResultHandler;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.ui.Model;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;  

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
//当然 你可以声明一个事务管理 每个单元测试都进行事务回滚 无论成功与否
@TransactionConfiguration(defaultRollback = true)
//记得要在XML文件中声明事务哦~~~我是采用注解的方式
@Transactional
public class ExampleTests {  

    @Autowired
    private WebApplicationContext wac;  

    private MockMvc mockMvc;  

    @Before
    public void setup() {
        // webAppContextSetup 注意上面的static import
        // webAppContextSetup 构造的WEB容器可以添加fileter 但是不能添加listenCLASS
        // WebApplicationContext context =
        // ContextLoader.getCurrentWebApplicationContext();
        // 如果控制器包含如上方法 则会报空指针
        this.mockMvc = webAppContextSetup(this.wac).build();
    }  

    @Test
        //有些单元测试你不希望回滚
        @Rollback(false)
    public void ownerId() throws Exception {
        mockMvc.perform((get("/spring/rest/4.do"))).andExpect(status().isOk())
                .andDo(print());
    }  

    @Test
    public void test() throws Exception {
        mockMvc.perform((get("/spring/test.do"))).andExpect(status().isOk())
                .andDo(print())
                .andExpect(model().attributeHasNoErrors("teacher"));
    }  

    @Test
    public void testb() throws Exception {
        mockMvc.perform((get("/spring/testb.do"))).andExpect(status().isOk())
                .andDo(print());
    }  

    @Test
    public void getAccount() throws Exception {
        mockMvc.perform((post("/spring/post.do").param("abc", "def")))
                .andExpect(status().isOk()).andDo(print());
    }  

}  
时间: 2024-08-08 19:29:17

spring mvc 单元测试示例的相关文章

spring MVC入门示例(hello world demo)

1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框 架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的. Spring Web MVC也是服务到工作者模式的实现,但进行可优化.前端控制器是DispatcherServlet:应用控制器其实拆为处理器映射器(Handler M

Spring Boot 单元测试示例

Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的单元测试. 在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启和使用它. 在pom.xml文件中引入maven依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artif

Spring MVC 入门示例讲解

MVC框架是什么 Dispatcher Servlet(Spring控制器) Spring入门示例 运行时依赖库 web.xml和spring-servlet.xml文件配置 请求控制器EmployeeController.java 视图模型EmployeeVO.java Dao类 业务层类 视图employeeListDisplay.jsp MVC框架是什么 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型.视图及控制器在应用程序中的角色将业务

Spring MVC 完整示例

在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简化它的配置. MVC框架是什么 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型.视图及控制器在应用程序中的角色将业务逻辑从界面中解耦.通常,模型负责封装应用程序数据在视图层展示.视图仅仅只是展示这些数据,不包含任何业务逻辑.控制器负责接收来自用户的

IDEA+Maven+Spring MVC HelloWorld示例

用Maven创建Web项目 选择webapp模板 创建成功后点Enable Auto-Import idea给我们创建出来的结构是这样的,这还不标准,需要自己修改. 在main文件夹下创建java文件夹,这是放置源码的地方,标记为sources.创建resources文件夹且标记为resource. 初始结构如下. 配置Spring pom.xml的配置 在<dependecies>与</dependecies>之间插入以下代码,添加依赖包. <dependency>

Spring MVC详细示例实战教程【转】

一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!--configure the setting of springmvcDispatcherServlet and configure the mapping--> <servlet>     <servlet-name>

spring mvc 注解示例

springmvc.xml <?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:mvc="http://www.springframewo

Spring MVC 单元测试异常 Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file

Sping 3.2.8.RELEASE + sping mvc + JDK 1.8运行异常. java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springfr

spring mvc 入门示例

classpath <?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.e