为什么进行单元测试?

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. 关于在MyEclipse中页面中文乱码的问题

    1.首先在Window>preferences>General>Workspace中改为UTF-8. 2.将项目的Properties>Resource改为UTF-8. 3.将 ...

  2. Dreamweaver使用过程的小技巧

    在用Dreamweaver中制作网站的过程中,经常会遇到这样的问题,我们要修改一些属性类的标签,如<a href="abc/def.html">,如果我们要把href= ...

  3. HTML+CSS Day06 超链接的样式与搭配

    1.格式 a:link    {} 普通的.未被访问的链接 a:visited {}  用户已访问的链接 a:hover  {} 鼠标指针位于链接的上方 a:active{} 链接被点击的时刻 2.常 ...

  4. Log4J积累

    1.常用级别,从低到高:DEBUG<INFO<WARN<ERROR 2.程序会打印比设置的级别高的日志信息(包括当前设置的日志级别).设置的级别越高,打印的日志信息越少. 3.if ...

  5. contentHorizontalAlignment 属性浅析

    转载自:http://blog.csdn.net/s0228g0228/article/details/46832285 最近在iOS 7以上总是碰到导航条上左右按钮距离边距太大的问题 为了解决这个这 ...

  6. ntp-keygen.c

    这个程序产生加密数据文件使用的的密码,遵循Autokey security protocol和NTPv4.文件名被名字和创建时间组成的头部当做前缀,后面跟有一个类型定义的描述符标签和PEM加密的数据结 ...

  7. 《CSS设计指南》阅读笔记

    一.HTML实体 HTML实体常用于生成那些键盘上没有的印刷字符.以一个和号(&)开头,一个分号(:)结尾,二者之间是表示实体的字符串. 如:“左引号(")     ”右引号(&qu ...

  8. Lucene.NET中Field.Index 和 Field.Store的几种属性的用法

    转载自 http://blog.csdn.net/yja886/article/details/6612069 lucene在doc.add(new Field("content" ...

  9. 转:selenium webdriver 执行javascript代码

    在用selenium webdriver 编写web页面的自动化测试代码时,可能需要执行一些javascript代码,selenium本身就支持执行js,我们在代码中import org.openqa ...

  10. HDU 5718 Oracle

    如果非零的数小于等于1个,则无解.否则有解. 取出一个最小的非零的数作为一个数,剩下的作为一个数,相加即可. #include<cstdio> #include<cstring> ...