Spring MVC(三)基于上一篇使用JUnit编写测试用例
来源:互联网 发布:测评软件 编辑:程序博客网 时间:2024/05/22 01:54
基于Spring MVC(二)工程中的HelloController.java控制器进行JUnit测试
在HelloController.java上右击--->new--->JUnit---->JUnit Test Case,之后项目结构如下:
首先看一下HelloController.java的代码:
package com.liu.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/test")public class HelloController {@RequestMapping("/index.do")public String users(HttpServletRequest request,HttpServletResponse response) throws Exception{String username = request.getParameter("username");String password = request.getParameter("password");System.out.println("----:"+username);System.out.println("----:"+password);request.setAttribute("username", username);request.setAttribute("password", password);return "/index";}}输入主要是username和password,然后返回字符串"/index",意思就是用index.jsp进行视图渲染。
由于我们对这里输入的username和password不做任何要求,只要我们输入它们,便返回"/index"
因此,JUnit测试代码HelloControllerTest.java如下:
package com.liu.controller;import static org.junit.Assert.*;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.mock.web.MockHttpServletRequest;import org.springframework.mock.web.MockHttpServletResponse;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** *@RunWith:使用pringJUnit4ClassRunner.class进行测试 *@ContextConfiguration:加载配置文件 */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"classpath*:/springmvc-servlet.xml","classpath*:/web.xml"})public class HelloControllerTest {//模拟request,responseprivate MockHttpServletRequest request;private MockHttpServletResponse response;private HelloController helloController;/* * 测试开始之前进行初始化 */@Beforepublic void setUp() throws Exception {request = new MockHttpServletRequest();request.setCharacterEncoding("UTF-8");response = new MockHttpServletResponse();}@Testpublic void testUser() {request.setParameter("username", "admin");request.setParameter("password", "123456");try {//判断控制器执行后是否返回字符串"/index"用于渲染assertEquals("/index", helloController.users(request, response));} catch (Exception e) {e.printStackTrace();}}}
然后右击HelloControllerTest.java--->Run as--->JUnit Test,运行通过:
0 0
- Spring MVC(三)基于上一篇使用JUnit编写测试用例
- spring mvc 使用junit 测试 controller
- 基于JUnit测试Spring MVC的正确方法。(仅限Spring 3.2)
- Eclipse中使用Junit编写测试用例
- 基于Spring的Junit测试
- Spring 使用Junit的MockMvc 写测试用例
- JUnit 编写测试用例的建议
- springboot + mybatis +junit 测试用例编写
- Spring MVC参数化测试 - Junit Parameterized
- Apache Ant+Junit对Java(Spring MVC(基于注解))工程进行测试(二)
- 在spring中编写junit测试单元
- 使用junit测试用例
- 基于注解的 Spring MVC(上)
- 使用 Spring 2.5 基于注解驱动的 Spring MVC(三)
- 使用junit进行Spring测试
- 基于spring和mybatis项目的JUnit测试用例的实现
- Junit 编写测试用例测试WebService方法
- 基于spring 3 注解的junit测试
- SOLID
- 云主机是什么?
- 习题2-5 分数化小数(decimal)
- hadoop学习之hadoop完全分布式集群安装
- javascript设计模式实践之策略模式--输入验证
- Spring MVC(三)基于上一篇使用JUnit编写测试用例
- .net 对象浅拷贝和深拷贝的实现
- error LNK2019: 无法解析的外部符号 _Direct3DCreate9@4,该符号在函数 "int __cdecl InitD3D(struct HWND__ *,unsigned lo
- 【Android】AsyncTask原理应用及源码关键部分解析
- 《剑指Offer》读书笔记05:用两个栈实现队列
- 【Struts2】(3)参数传递
- linux 查看库依赖关系
- MySQL分片
- 如何保证网页的字体在各平台都尽量显示为最高质量的黑体?