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注解,则表名该方法 ...
随机推荐
- PHP数学函数试题
1.求绝对值的函数是什么? 2.在任意进制之间转换数字的函数是什么? 3.二进制转换为十进制,十进制转换为二进制,十六进制转换为十进制,十进制转换为十六进制,八进制转换为十进制,十进制转换为八进制的函 ...
- QML Flipable、Flickable和状态与动画 下篇
本文介绍的是Flickable和状态与动画,我们以前接触过QML相关的内容,那么本文介绍的内容就很明了了.先来看内容. AD: Flickable和状态与动画 下篇是本节要介绍的内容,Flickabl ...
- CSS中常见中文字体的英文名称(Microsoft YaHei,SimHei)
Mac OS的一些: 华文细黑:STHeiti Light [STXihei]华文黑体:STHeiti华文楷体:STKaiti华文宋体:STSong华文仿宋:STFangsong儷黑 Pro:LiHe ...
- Java学习笔记之I/O
package com.chinasofti.javase20160819; import java.io.BufferedReader; import java.io.BufferedWriter; ...
- wpf中ListBox的选中项与ComboBox间的绑定
产品类: public class Product:NotificationObject { private int productID; public int ProductID { get { r ...
- 【转】configure/make/make install的使用说明
这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤. ./configure是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不是需要CC或GCC ...
- 推荐 iOS 网站:
0. https://developer.apple.com/videos/1. http://www.raywenderlich.com2. http://nshipster.com3. http: ...
- 运行第一个SparkKPI程序
1.复制一个examples中SparkPi.scala到IntelliJ IDEA编辑器,运行,出现错误: “org.apache.spark.SparkException: A master UR ...
- GameUnity 2.0 发布倒计时
万众期待的 gameunity 网络游戏框架 已经完成了,现在在最后的检验调试阶段. 因为版本 改动非常之大,所以 版本号 从0.2版本 改成 2.0版本. gameunity事件部分,一如既往保持高 ...
- org.apache.lucene.queryParser.ParseException: Encountered "<EOF>" at line 1, column 0.
如果出现了下列错误,那是因为用错了函数.把queryParser.Query改称queryParser.parse就通过了 org.apache.lucene.queryParser.ParseExc ...