为什么进行单元测试?

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. java动态代理(1)

    来源:http://www.cnblogs.com/jqyp/archive/2010/08/20/1805041.html# java动态代理(JDK和cglib) JAVA的动态代理 代理模式 代 ...

  2. 在idea的maven项目使用el或jstl表达式

    必须加上这句: <%@ page isELIgnored="false" %> 否则无法解析el或jstl表达式 <%@ taglib prefix=" ...

  3. 【jsp 练习】 给定三角形三边判断是否能组成三角形及计算面积

    Test.java package package1; public class Test { double side1 = -1 , side2 = -1 , side3 = -1 , area = ...

  4. Webkit之资源加载

    一.webkit资源分类 webkit中有多种资源,大致分为以下几种: HTML文本 CSS样式文本 - CachedCSSStyleSheet 字体 - CachedFont 图片 - Cached ...

  5. aX+bY+cZ=n(非负整数解存在性)

    题意: a*1234567+b*123456+c*1234=n 非负整数解得存在性. 题解: 看代码. #include<iostream> #include<cstdio> ...

  6. Spring中管理Bean以及解析XML

    Spring是分层的轻量级框架 以IoC(Inverse of Control 反转控制)和AOP(Aspect Oriented Programming 面向切面编程)为核心 应用Spring的好处 ...

  7. [转]new一个Object对象占用多少内存?

    我们分解下ArrayList arr = new ArrayList();等同于ArrayList arr = null;//初始化arr = new ArrayList();//实例化这两个过程.初 ...

  8. Air打包exe

    1.用flash创建一个airtest.fla,发布目标选择为AIR.ctrl+enter会得到如下文件: 2.把flex sdk的bin中找到adl.exe,复制过来,放置到:项目目录\bin\ad ...

  9. 2016中国大学生程序设计竞赛 - 网络选拔赛 1011 Lweb and String

    Problem Description Lweb has a string S. Oneday, he decided to transform this string to a new sequen ...

  10. CodeForces 678D Iterated Linear Function

    简单矩阵快速幂. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm& ...