[转][Java]使用Spring配合Junit进行单元测试的总结
http://www.51testing.com/html/14/n-1408814.html
1.直接对spring中注入的bean进行测试(以DAO为例):
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:spring-config-test.xml"})
public class TestProjectDao {
@Autowired
ProjectDao projectDao;
@Test
public void testCreateProjectCode(){
long applyTime = System.currentTimeMillis();
Timestamp ts = new Timestamp(applyTime);
Map codeMap = projectDao.generateCode("5", "8",ts,"院内");
String projectCode = (String)codeMap.get("_project_code");
Timestamp apply_time = (Timestamp)codeMap.get("_apply_time");
System.out.print(projectCode);
System.out.print(apply_time.toString());
Assert.assertTrue(projectCode.length()==12);
}
package com.jiaoyiping.baseproject;
import com.jiaoyiping.baseproject.privilege.controller.MeunController;
import com.jiaoyiping.baseproject.training.bean.Person;
import junit.framework.Assert;
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.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.ModelAndView;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Created with IntelliJ IDEA.
* Date: 14-9-25
* Time: 下午6:45
* To change this template use File | Settings | File Templates.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
//@ContextConfiguration(classes = {WebMvcConfig.class, MockDataConfig.class})
@ContextConfiguration(locations={"classpath:/spring/applicationContext.xml", "classpath*:mvc-dispatcher-servlet.xml"})
public class TestMockMvc {
@Autowired
private org.springframework.web.context.WebApplicationContext context;
MockMvc mockMvc;
@Before
public void before() {
//可以对所有的controller来进行测试
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
//仅仅对单个Controller来进行测试
// mockMvc = MockMvcBuilders.standaloneSetup(new MeunController()).build();
}
@Test
public void testGetMenu(){
try {
System.out.println("----------------------------");
ResultActions actions =
this.mockMvc.perform(get("/menu/manage.action"));
System.out.println(status());//
System.out.println(content().toString());
actions.andExpect(status().isOk());
// actions.andExpect(content().contentType("text/html"));
System.out.println("----------------------------");
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
如果想使用TestNG,可以参考http://www.51testing.com/html/26/n-856526.html
如果想使用Spock,可以参考http://stackoverflow.com/questions/9811345/how-to-inject-spring-beans-into-spock-test
[转][Java]使用Spring配合Junit进行单元测试的总结的更多相关文章
- 使用Spring配合Junit进行单元测试的总结
最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注 ...
- spring集成Junit做单元测试及常见异常解决办法
spring-test依赖包 <!--Spring-test --> <!-- https://mvnrepository.com/artifact/org.springframew ...
- Spring boot Junit Test单元测试
Spring boot 1.40 JUnit 4 需要依赖包 spring-boot-starter-test.spring-test 建立class,加上如下注解,即可进行单元测试,别的帖子里说要加 ...
- Spring整合Junit进行单元测试
I. 加入依赖包 Spring Test (如spring-test-2.5.4.jar) JUnit 4 Spring 其他相关包 II.新建Junit Test Case III.读取配置文件 @ ...
- 中阶d03.3 JDBC_CURD_Util --- 使用 junit执行单元测试(增删改查)
1.单元测试环境准备 https://www.cnblogs.com/longesang/p/11399010.html 2.测试 3.结果返回 4.代码 新建一个test目录统一存放测试案例 查: ...
- Spring完全基于Java配置和集成Junit单元测试
要点: 配置继承WebApplicationInitializer的类作为启动类,相当于配置web.xml文件 使用@Configuration注解一个类,在类中的方式使用@Bean注解,则表名该方法 ...
- Spring集成JUnit单元测试框架
一.JUnit介绍 JUnit是Java中最有名的单元测试框架,用于编写和运行可重复的测试,多数Java的开发环境都已经集成了JUnit作为单元测试的工具.好的单元测试能极大的提高开发效率和代码质量. ...
- 学习 Spring Boot:(二十九)Spring Boot Junit 单元测试
前言 JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量. JUnit 测试框架具有以下重要特性: 测试工具 测试套件 测试运行器 测试分类 了 ...
- 聊聊单元测试(三)——Spring Test+JUnit完美组合
本着“不写单元测试的程序员不是好程序员”原则,我在坚持写着单元测试,不敢说所有的Java web应用都基于Spring,但至少一半以上都是基于Spring的. 发现通过Spring进行bean管理后, ...
随机推荐
- 367. Valid Perfect Square判断是不是完全平方数
[抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- 599. Minimum Index Sum of Two Lists两个餐厅列表的索引和最小
[抄题]: Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of fa ...
- 面试题:TCP协议三次握手
一.首先了解TCP报文格式 其中必须了解的字段有: 1.源端口与目的端口:16位,标识出发送端与接收端的端口号. 2.序号:32位,也叫顺序号.seg序号,本报文段所发送的数据的第一个字节的序号,用来 ...
- 写一段php代码,确保报个进程同时写入同一个文件
- hdu 2206 IP的计算(最全的注意事项)
注意: 1.非法字符(包括空格) 如下都是错的 A.145.124.4 192.168.18 123(用scanf输入的话,则接收的是:192.168.18) 2.'.'有且仅有3个,且不能连续出现, ...
- Edge 自动给数字加下划线的问题
<meta name="format-detection" content="telephone=no,email=no,address=no">
- 高级软件测试技术(测试管理工具实践day4)
今天是截止日期,有胡俊辉的指导下小组成员都了解使用了Bugzilla的基本使用.大家都在晚上之前把各自的文档汇总给汪鸿,由他撰写了操作手册.并且在下午杨瑞丰完成了视频的录制工作.但是在转化为MP4 格 ...
- Mapper配置文件夹
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-// ...
- POJ3026 Borg Maze(bfs求边+最小生成树)
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...