JUnit----单元测试
为什么进行单元测试?
1. 重用测试, 应付将来实现的变化.
2. 明确指定我的东西是没问题的.
Failure, error的区别?
Failure只测试失败, Error指程序本身出错
1. new ->java project: JUnit4
2. src右键->new->class T, package: com.bjsxt.junit4
package com.bjsxt.junit4;
public class T {
public int add(int x, int y){
return x+y;
}
public int divide(int x, int y){
return x/y;
}
}
3. 包com.bjsxt.junit4 右键-> new->package-> com.bjsxt.junit4.test ->右键-> new->JUnit test case(New JUnit 4 test, name: TTest, class under test:T)->next -> 选择要测试的T方法->finish->选择默认的myeclipse 的jar 包,
4. build path->remove ... 删除原生jar, 添加自己下载的jar, build-> add external archieves.
5. 打开TTest.java,编写测试代码:
package com.bjsxt.unit4.test; import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*; import org.junit.Test; import com.bjsxt.u2.T; public class TTest { @Test
public void testAdd() {
int z = new T().add(5,3);
assertThat(z,is(8));
assertThat(z, allOf(greaterThan(1), lessThan(10)));
}
}
keep bar green to keep bugs clean.
如果不先删除包, 会出现如下错误:
1. 使用上述is方法时提示错误, 需要引入hamcrest jar包(core和license包): 右键工程-> build path ,引入两个包
然后发现出现: java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package
因为hamcrest的包和junit4里的hamcrest包的classloader用的不是同一个.解决方法:
把myeclipse自带的包都去掉, 引入自己的包junit即可.
如果测试divide时出现异常怎么办?
在annotation里加入expected的异常类型即可. 后面timeout是时间的判断
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z=new T().divide(5,0);
}
Annotation:
1. @Test : 测试方法
2. @Ignore: 被忽略的测试方法
3. @Before: 每个测试方法之前运行 用法: 执行方法前打开某个文件
4. @After: 每个测试方法之后运行 用法: 执行方法后关闭某个文件
5. @BeforeClass: 所有测试方法之前运行 用法: 配置工作, 建立数据库连接等费时工作
6. @AfterClass: 所有测试方法之后运行 用法: 比如关闭数据库
例子:
package com.bjsxt.junit4.test; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import com.bjsxt.junit4.T; public class TTest { @BeforeClass //提前配置工作, 比如数据库连接等耗费时间的资源, 搭载复杂环境时
public static void beforeClass(){
System.out.println("BeforeClass");
} @AfterClass //关闭数据库连接
public static void afterClass(){
System.out.println("AfterClass");
} @Before //打开文件使用
public void before(){
System.out.println("before"); }
@Test
public void testAdd() {
int z=new T().add(5,3);
assertThat(z,is(8));
//assertTrue("z is not bigger than 3",z>3);
} //@Ignore
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z=new T().divide(5,0);
} @After //关闭文件使用
public void after(){
System.out.println("after"); }
}
结果:
BeforeClass
before
after
before
after
AfterClass
还可以新建另一个类, User:
package com.bjsxt.junit4;
public class User {
public String getName(){
return "songhuiqiao";
}
}
在test包右键, 新建unit test, UserTest
package com.bjsxt.junit4.test; import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.is; import org.junit.Test; import com.bjsxt.junit4.User; public class UserTest { @Test
public void testGetName() {
assertThat(new User().getName(),is("songhuiqiao"));
} }
如果两个类同时运行的话, 在test包右键, run->configuration->run all:
JUnit----单元测试的更多相关文章
- junit单元测试(keeps the bar green to keeps the code clean)
error是程序错误,failure是测试错误. junit概要: JUnit是由 Erich Gamma (设计模式的创始人)和 Kent Beck (敏捷开发的创始人之一)编写的一个回归测试框架( ...
- spring && Cobertura && maven &&junit 单元测试以及测试覆盖率
1. 目的: junit 单元测试,Cobertura 测试覆盖率报告 项目目录结构 2. maven 配置 <project xmlns= ...
- 解决Junit单元测试 找不到类 ----指定Java Build Path
做junit 单元测试时,发现怎么执行都是以前编译过得代码. 最后找到原因了, src/test/java 编译完的.class路径是 Default output folder Default ou ...
- JUnit单元测试框架的使用
http://blog.csdn.net/mao520741111/article/details/51462215 原文地址 http://www.open-open.com/lib/view/op ...
- Java 工具 JUnit单元测试
Java 工具 JUnit单元测试 @author ixenos 1.1. JUnit单元测试框架的基本使用 一.搭建环境: 导入junit.jar包(junit4) 二.写测试类: 0,一般一个 ...
- Spring框架中整合JUnit单元测试的方法
一. 步骤: 1. 拷贝jar包: 1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ; 2. 替换原来的main函数: 1. 在测试类上使用注解方式替换: ...
- spring框架学习(三)junit单元测试
spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...
- 备忘:Junit单元测试
junit 目前测试都是在main方法中调用目前的结果都需要人工对比是否是想要的 1.使用Junit测试方法,绿色条条代表方法测试成功,没有bug,如果是红色条条代表有异常,测试不通过2.点击方法名. ...
- 单元测试系列:JUnit单元测试规范
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6762032.html Junit测试代 ...
- Spring完全基于Java配置和集成Junit单元测试
要点: 配置继承WebApplicationInitializer的类作为启动类,相当于配置web.xml文件 使用@Configuration注解一个类,在类中的方式使用@Bean注解,则表名该方法 ...
随机推荐
- CentOS6.6 部署Apache+Svn
svn代码 目前大多数公司 管理代码都是用这个 这个比较方便简单,git用的人数也比较多,我们下面来部署一下这个程序 svn+apache集成 系统环境 # cat /etc/redhat-relea ...
- IIS访问PHP文件时,弹出用户名和密码提示框的解决方法
找了一圈,以下的方法解决了IIS访问PHP弹用户名和密码提示框问题. 解决方法:给PHP安装目录everyone读取权限 这样不知道会不会出现安全问题,请大家谨慎.
- gpg XXX NO_PUBKEY XXX
手动添加PPA源遇到找不到公钥的情况 1. gpg --keyserver keyserver.ubuntu.com --recv-keys XXX gpg --armor --export XXX ...
- 天天果园,中粮我买网等生鲜APP竞品分析
奈何对生鲜行业的品类,价格,供应链不熟悉,想先从APP开始来了解生鲜行业和各个生鲜企业,若有不足之处,还望海量,也请帮忙指正. 选取了以下竞品:天天果园,易果生鲜,一米鲜,中粮我买网,爱鲜蜂,每日优鲜 ...
- ActiveX控件在IE中不响应Backspace消息
1.操作输入法需要导入: #include <imm.h> #pragma comment(lib, "imm32") 2.定义变量: //键盘钩子句柄 HHOOK g ...
- 43个优秀的Swift开源项目推荐(转载)
["轮子"] 工具类 SwiftyJSON:GitHub 上最为开发者认可的 JSON 解析类 Dollar.swift:Swift 版 Lo-Dash (或 underscore ...
- AS-->如何更高效的使用 Gradle, 快速build apk
版权声明:欢迎转载,转载请注明出处;http://blog.csdn.net/angcyo 看本文之前,推荐先看我之前写的一篇文章: 传送门 日前Android Stuido 已经更新到 2.0.0 ...
- SpringMVC中获得HttpRequest对象的方法
1. 使用@autowired注入HttpRequest 2. 在方法中直接声明形参有HttpRequest即可. 3. 使用一个Listener,然后获取.
- Mysql程序
drop table if exists comp_ap; create table comp_ap as select ProjectName, ModelCode, 'AP_ACMClosed' ...
- 使用curl获取乱码问题
今天通过curl获取百度地图接口数据,获取到居然是乱码,于是我查看是不是编码问题,发现返回的编码和自己的编码都是utf-8, 继续找原因,发现header报文中 Content-encoding 为 ...