为什么进行单元测试?

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----单元测试的更多相关文章

  1. junit单元测试(keeps the bar green to keeps the code clean)

    error是程序错误,failure是测试错误. junit概要: JUnit是由 Erich Gamma (设计模式的创始人)和 Kent Beck (敏捷开发的创始人之一)编写的一个回归测试框架( ...

  2. spring && Cobertura && maven &&junit 单元测试以及测试覆盖率

    1. 目的:       junit 单元测试,Cobertura   测试覆盖率报告       项目目录结构          2. maven 配置     <project xmlns= ...

  3. 解决Junit单元测试 找不到类 ----指定Java Build Path

    做junit 单元测试时,发现怎么执行都是以前编译过得代码. 最后找到原因了, src/test/java 编译完的.class路径是 Default output folder Default ou ...

  4. JUnit单元测试框架的使用

    http://blog.csdn.net/mao520741111/article/details/51462215 原文地址 http://www.open-open.com/lib/view/op ...

  5. Java 工具 JUnit单元测试

    Java 工具 JUnit单元测试 @author ixenos 1.1.   JUnit单元测试框架的基本使用 一.搭建环境: 导入junit.jar包(junit4) 二.写测试类: 0,一般一个 ...

  6. Spring框架中整合JUnit单元测试的方法

    一. 步骤: 1. 拷贝jar包: 1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ; 2. 替换原来的main函数: 1. 在测试类上使用注解方式替换: ...

  7. spring框架学习(三)junit单元测试

    spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...

  8. 备忘:Junit单元测试

    junit 目前测试都是在main方法中调用目前的结果都需要人工对比是否是想要的 1.使用Junit测试方法,绿色条条代表方法测试成功,没有bug,如果是红色条条代表有异常,测试不通过2.点击方法名. ...

  9. 单元测试系列:JUnit单元测试规范

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6762032.html Junit测试代 ...

  10. Spring完全基于Java配置和集成Junit单元测试

    要点: 配置继承WebApplicationInitializer的类作为启动类,相当于配置web.xml文件 使用@Configuration注解一个类,在类中的方式使用@Bean注解,则表名该方法 ...

随机推荐

  1. 好题 线段树对数据的保存+离线的逆向插入 POJ 2887

    题目大意:给一个字符串,有插入和询问操作,每次往一个位置插入一个字符或者询问第p个位置的字符是什么. 思路:我们离线询问,逆向把所有的字符都插入给线段树,然后再查询就好了,每次都要记得插入线段树的最后 ...

  2. Oracle record 解锁

    Oracle修改表中记录时出现record is locked by another user的弹出框问题 是因为在操作表中数据时没有commit,导致表被锁. 执行下面两行语句: (1)查看被锁表的 ...

  3. 国内的cdn

    测试了一下,百度的非常快 ----------------------------------------------------------------------- 原文:https://www. ...

  4. background和background-position相关笔记

    background 可在一个声明中设置background-color,background-image,background-repeat,background-attachment,backgr ...

  5. 封装sdk API 应用

    1 #include "QWinApp.h" 2 #include "QGlobal.h" 3 int WINAPI _tWinMain(HINSTANCE h ...

  6. HDU 1155 Bungee Jumping 物理

    题目大意:给出k:绳子的劲度系数,l:绳长,s:桥高,w:邦德的质量,g取9.81.绳子弹力=形变量*劲度系数.如果落地速度大于10 则摔死,小于0则飘着空中. 题目思路:根据能量守恒得知:落地的动能 ...

  7. php杂乱

    //    //    if ( $_GET['action'] == 'search' ) {//        $_clean = array();//        $_clean['stype ...

  8. mysql 数字字段的类型选择

    bigint            从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8  ...

  9. offset获取位置

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  10. HDU1102--Constructing Roads(最小生成树)

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...