原文链接:http://blog.csdn.net/u013041642/article/details/71430293

Spring对Controller、Service、Dao进行Junit单元测试总结

原创 2017年05月08日 19:58:47
  • 8817

Spring对Controller、Service、Dao进行Junit单元测试总结

​ 所有用Junit进行单元测试,都需要下面的配置

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
  • 1
  • 2
  • 3

​ applicationContext.xml 是整个项目的Spring的配置文件。包括数据源配置、MVC配置和各种Bean的注册扫描。如果你是多个文件,就用都好隔开写多个,像这样

{ "classpath:applicationContext.xml","classpath:servlet-context.xml"}
  • 1

1.Dao层的单元测试。

​ 将*Mapper接口注入进来,直接调用即可。加上@Transactional 事物管理注解以后,单元测试执行完后会撤销对数据库的修改。想看增删改的结果的话,可以把这个注解先注释。

~~~java 
package testDao;

import com.susq.mbts.dao.UserMapper; 
import com.susq.mbts.domain.User; 
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.transaction.annotation.Transactional;

import java.util.Date;

/** 
* Created by susq on 2017-5-3. 
*/ 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {“classpath:applicationContext.xml”}) 
@Transactional 
public class DaoTests { 
@Autowired 
private UserMapper userMapper;

@Test
public void testSelect() {
User userInfo = userMapper.selectByPrimaryKey(1L);
System.out.println(userInfo);
} @Test
public void insert(){
User user = new User();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

// user.setId(5L); 
user.setAge(“9”); 
user.setName(“王五”); 
user.setSex(“M”); 
user.setCreateTime(new Date()); 
userMapper.insert(user); 
}

@Test
public void testUpdate() {
User user = new User();
user.setId(5L);
user.setAge("999");
user.setName("王五");
user.setSex("M");
user.setCreateTime(new Date()); userMapper.updateByPrimaryKey(user);
} @Test
public void testDelete() {
userMapper.deleteByPrimaryKey(7L);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

}

~~~

2. Service层的单元测试与Dao层基本一致,把Service注入进来调用就行。

~~~java 
package testDao;

import com.susq.mbts.domain.User; 
import com.susq.mbts.service.UserService; 
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;

/** 
* Created by susq on 2017-5-8. 
*/ 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {“classpath:applicationContext.xml”}) 
public class Sertest { 
@Autowired 
private UserService userService;

@Test
public void selectUserTest() {
User u = userService.selectUser(1);
System.out.println(u);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6


~~~

3. Controller层的单元测试

​ 一种是把Controller的Bean注入进来,调里面的方法,这很显然比较扯淡,连Url都没经过,只测了方法。另一种是使用MockMvc模拟通过url的接口调用。MockMvc是SpringMVC提供的Controller测试类,每次进行单元测试时,都是预先执行@Before中的setup方法,初始healthArticleController单元测试环境。 
​ 注意:一定要把待测试的Controller实例进行MockMvcBuilders.standaloneSetup(xxxxController).build(); 否则会抛出无法找到@RequestMapping路径的异常:No mapping found for HTTP request with URI [/cms/app/getArticleList] in DispatcherServlet

package testDao;

import com.susq.mbts.controller.UserController;
import org.junit.Before;
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.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; /**
* Created by susq on 2017-5-8.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class ConTest {
@Autowired
private UserController userController; private MockMvc mockMvc; @Before
public void setup(){
mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
} @Test
public void Ctest() throws Exception {
ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/show_user3").param("id", "1"));
MvcResult mvcResult = resultActions.andReturn();
String result = mvcResult.getResponse().getContentAsString();
System.out.println("=====客户端获得反馈数据:" + result);
// 也可以从response里面取状态码,header,cookies...
// System.out.println(mvcResult.getResponse().getStatus());
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

源码链接:http://download.csdn.net/detail/u013041642/9836760

Controller、Service、Dao进行Junit单元的更多相关文章

  1. Java Web基础——Controller+Service +Dao三层的功能划分

    转自:https://www.cnblogs.com/cielosun/articles/5752272.html 1. Controller/Service/DAO简介: Controller是管理 ...

  2. Controller Service Dao总结

    今天主要学习了Controller,Service,Dao的相关知识 我的理解主要是这种,Controller主要与前台页面打交道 比方:前台页面有一个"加入用户"的提交butto ...

  3. SpringBoot--Easycode、mybatisX插件生成entity,controller,service,dao,mapper IDEA版 项目提效神器

    一.介绍 Easycode是idea的一个插件,可以直接对数据的表生成entity,controller,service,dao,mapper,无需任何编码,简单而强大. MybatisX 是一款基于 ...

  4. Spring对Controller、Service、Dao进行Junit单元测试总结

    测试类注解 @RunWith(SpringRunner.class) @SpringBootTest(classes={DemoApplication.class}) 以Controller层的的单元 ...

  5. Spring+hibernate+JSP实现Piano的数据库操作---2.Controller+Service+Dao

    Controller package com.controller; import com.entity.Piano; import org.dom4j.rule.Mode; import org.s ...

  6. Spring Service、Dao进行Junit单元测试

    pring对Controller.Service.Dao进行Junit单元测试总结 ​ 所有用Junit进行单元测试,都需要下面的配置 @RunWith(SpringJUnit4ClassRunner ...

  7. Spring MVC 项目搭建 -2- 添加service ,dao,junit

    Spring MVC 项目搭建 -2- 添加service ,dao,junit 1.dao public class Hero { private String name; public Strin ...

  8. 使用spring注解@Controller @Service @Repository简化配置

    前言:在web项目中引入spring框架中的配置文件,我们给每一个java bean进行相关配置可以非常安全,便捷的管理我们的bean.那么,问题来了,如果一个项目中所涉及到的java bean十分庞 ...

  9. [转]Java Web基础——Action+Service +Dao三层的功能划分

    原文地址:http://blog.csdn.net/inter_peng/article/details/41021727 参考来源:http://www.xuebuyuan.com/2153333. ...

随机推荐

  1. 赚钱的小生意,VC对你没兴趣

    创业者,赚钱的生意就不要去找VC(风险投资)了,因为人家对你的生意没有兴趣. 无论是创业者,VC,股权投资散户,都需要对一个"生意"的规模有个总体的认识. 就"生意&qu ...

  2. 关于Trie的一些算法

    最近学习了一下关于Trie的一些姿势,感觉很实用. 终于不用每次看到字符串判重等操作就只想到hash了 关于Trie的定义,来自百度百科 在计算机科学中,Trie,又称前缀树或字典树,是一种有序树状的 ...

  3. 【转】从Shell脚本内部将所有标准输出及标准错误显示在屏幕并同时写入文件的方法

    如果全部都要重定向的话每一条命令后面>>并不方便,可以这么做.在开头就声明 exec 1>>$log_file表示将脚本中所有的正确输出全部追加到$log_file,错误信息会 ...

  4. libgdx学习记录24——九宫格NinePatch

    NinePatch用于图片纹理拉伸显示.当图片拉伸时,4个角不会拉伸,而只有中间的部分会拉伸,适合做圆角矩形类的Button. 简单示例: package com.fxb.newtest; impor ...

  5. libgdx相关知识点

    Gdx.graphics.setContinuousRendering(false); 设置图像为非连续自动渲染. 设置Opengl的混合模式,支持alpha属性 Gdx.gl.glBlendFunc ...

  6. python删除文件与目录的方法

    python内置方法删除目录(空目录与非空目录)及文件 1.os.remove(file_path):删除文件 #PPTV是文件夹,xen.txt是文件 >>> os.remove( ...

  7. 使用VSCode调试单个PHP文件

    突然发现是可以使用 VSCode 调试单个 PHP 文件的,今天之前一直没有弄成功,还以为 VSCode 是不能调试单文件呢.这里记录一下今天这个"突然发现"的过程. 开始,是在看 ...

  8. 用10分钟,搭建图像处理编程环境,0失败!(python语言,windows系统)

    以前,你可能看过很多的文章,开始搭建一个图像处理的编程环境. 结果,按照教程一步一步做的时候,总是出现各种各样的问题. 就算成功了,后续开发过程中要用到不同版本的opencv,不同版本python,更 ...

  9. 通过blockchain_go分析区块链交易原理

    原文链接-石匠的Blog 1.背景 在去中心化的区块链中进行交易(转账)是怎么实现的呢?本篇通过blockchain_go来分析一下.需要进行交易,首先就需要有交易的双方以及他们的认证机制,其次是各自 ...

  10. SQL邮件服务(解决各种疑难杂症)+案例 + 使用SQLserver 邮件系统发送SQL代理作业执行警告

    首先你需要知道你要做的几部: 1 每个数据库都有自己的 SERVICE BROKER 很多SQL SERVER内部服务依赖它 2 启动 SERVICE BROKER 需要 1 STOP 你的 SQL  ...