- 浏览: 5134803 次
- 性别:
- 来自: 天津
博客专栏
-
实战 Groovy
浏览量:29044
文章分类
- 全部博客 (639)
- 代码之谜 (6)
- JavaScript quirks (5)
- 程序员 (92)
- Java (93)
- BT编程 (7)
- html/css (64)
- Groovy&Grails (42)
- Android (20)
- C/C++ (5)
- PHP/Perl/Python (46)
- 经典文章 (51)
- CodeIgniter (14)
- JQuery (10)
- 笑话 (4)
- 其他 (32)
- javascript (69)
- 云计算 (0)
- html5 (7)
- 面试 (8)
- google (3)
- nosql (2)
- nodejs (11)
- go (5)
- erlang (1)
- 小常识 (3)
- 冷知识 (5)
- database (4)
- web (12)
- 架构 (12)
- Exception (0)
最新评论
-
jqw1992:
https://www.chromefor.com/libra ...
[福利] 开发者必备的 Chrome 插件——ChromeSnifferPlus -
litjerk:
初步算了一下,目前最最精简的Win98版是5M,他5个小时多敲 ...
让人目瞪口呆的三位世界级电脑大师 -
379855529:
。。似乎重点没说NIO啊,前面基础只是铺垫的很好的,可是我要的 ...
Java NIO与IO的详细区别(通俗篇) -
springmvc_springjpa:
spring mvc demo教程源代码下载,地址:http: ...
一步步开发 Spring MVC 应用 -
匡建武:
Good
四个程序员的一天
为了说明JUnit4
和JUnit3
的区别,我们先看代码:
Largest.java:
这是一个测试类
//测试类
public class Largest {
public Largest() {
}
public static int largest(int[] list){//用于求该数组的最大值,为了测试方便,该方法为静态方法
int index,max=Integer.MAX_VALUE;
for(index=0;index<list.length-1;index++){
if(list[index]>max){
max=list[index];
}
}
return max;
}
}
首先看JUnit3
的
测试用例:TestLargest.java
:
import junit.framework.TestCase;
//这是用Junit3创建的,没有报错。
public
class TestLarget extends TestCase
{
protected void setUp()
throws Exception {
super.setUp();
}
public void testSimple()
{
assertEquals
(9,
Largest.largest(new int[]{9,8,7}));
}
protected void tearDown()
throws Exception {
super.tearDown();
}
}
然后我们再看用JUnit4
的测试用例:TestLargest.java:
//注意下面的包org.junit.After,org.junit.Before
import
org.junit.After;
import org.junit.Before;
//为了测试自己写的脚本,要引入包:
import org.junit.Test;
import org.junit.Assert;//assertEquals方法等都在Assert.中
//此种是自己New的一个Junit Test Case,
然后选择了自动添加setUp和tearDown方法。注意在两个方法的前面有两个标记:@Before和@After
public class TestLargest {
@Before
public void setUp() throws Exception {
}
@Test //此处必须加
public void testSimple()
{
//assetEquals(9,Largest.largest(new
int[]{9,8,7}));//为什么assetEquals()报错呢
Assert.assertEquals(
9,Largest.largest(new
int[]{9,8,7}));//避免上面的错误,要用此种形式
}
@After
public void tearDown() throws Exception {
}
}
下面的这个是右键
Largest.java,New->JUnit Test Case,自动生成的测试框架(JUnit4),LargestTest.java:
import static org.junit.Assert.*;
import org.junit.Test;
//此种方法为自动生成的框架。然后填写代码即可。右键
Larget.java,New->Junit Test Case
public class LargestTest {
@Test
public void testLargest() {
fail("Not yet implemented");
}
}
然后我们自己添加代码即可。 public class Product { public Product(String title, double
price) { public String getTitle() { public double getPrice() { public boolean equals(Object o) { return false; }
import java.util.*;
public class ShoppingCart public void removeItem(Product item)
throws ProductNotFoundException { public int getItemCount() { public void empty() { public ProductNotFoundException() { import
org.junit.After; private Product p; @After } import org.junit.After; private ShoppingCart cart; @After
@Test
Product book2 = new
Product("Pragmatic Project Automation", 29.95); Assert.
assertEquals(2, cart.getItemCount());
double expectedBalance =
book1.getPrice() + book2.getPrice(); cart.removeItem(book1); @Test Product book3 = new
Product("Pragmatic Unit Testingx", 29.95); Assert.
fail("Should raise a ProductNotFoundException");
}
catch(ProductNotFoundException expected) { import org.junit.runner.RunWith; //通过下面的形式运行套件,必
须构造一个空类 public class TestSuite {
}
import
junit.framework.Test; public static Test suite() { } import
junit.framework.*; } cart = new
ShoppingCart();
book1 = new
product("Pragmatic Unit Testing", 29.95);
cart.addItem(book1); /** cart.empty();
assertEquals
(0,
cart.getItemCount()); product book2 =
new product("Pragmatic Project Automation", 29.95); assertEquals
(2,
cart.getItemCount());
double
expectedBalance = book1.getPrice() + book2.getPrice();
cart.removeItem(book1);
assertEquals
(0,
cart.getItemCount()); try {
product
book3 = new product("Pragmatic Unit Testingx", 29.95); fail("Should
raise a ProductNotFoundException");
}
catch(productNotFoundException expected) { import junit.framework.*;
public class
TestClassComposite {//be a base class,extendding from TestCase is of no
necessary.
有上面的代码对比,我们可以总结JUnit4和JUnit3的区别
主
要有两点:
1. JUnit4
利用了
Java 5
的新特性
"
注释
"
,每个测试方法都不需要以
testXXX
的方式命名
,
运
行时不在用反射机制来查找并测试方法,取而带之是用
@Test
来标注每个测试方法,效率提升
2. JUnit4中
测试类不必
继承
TestCase
了,
另外要注意JUnit4和JUnit3引入的包完全不同。
PS:在
Eclipse
中要使用
Junit
的话,必须要添加
Junit
的
library
。
3.JUnit4和JUnit3在测试Suite时也有很大不同:
例如我们有两个测试类Product.java
,ShoppingCart.java
,还涉及到一个异常类ProductNotFoundException.java:
Product.java:
private String title;
private double price;
this.title = title;
this.price = price;
}
return title;
}
return price;
}
if (o instanceof Product) {
Product p = (Product)o;
return p.getTitle().equals(title);
}
}
ShoppingCart.java:
{
private ArrayList items;
public ShoppingCart()
{
items=new ArrayList();
}
public double getbalance()
{
double balance=0.00;
for(Iterator i=items.iterator();i.hasNext();)
{
Product item=(Product)i.next();
balance+=item.getPrice();
}
return balance;
}
public void addItem(Product item) {
items.add(item);
}
if (!items.remove(item)) {
throw new ProductNotFoundException();
}
}
return items.size();
}
items.clear();
}
}
ProductNotFoundException.java:
public class ProductNotFoundException
extends Exception {
super();
}
}
下面是用JUnit4
写的测试类:
ProductTest.java:
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;
public class ProductTest extends TestCase//extends完全可以不写,只是为了把测试方法加入到suite中
{
public ProductTest() {
}
//这是为了AllTests类做的铺垫
public ProductTest(String method)
{
super(method);
}
@Before
public void setUp() throws Exception {
p=new Product("a book",32.45);
}
public void tearDown() throws
Exception {
}
@Test
public void testGetTitle()
{
Assert.
assertEquals("a
book",p.getTitle());
}
@Test
public void testSameRefrence()
{
//product q=new product("a sheet",12.56);
Product q=p;
Assert.
assertSame("not
equale object",p,q);
}
@Test
public void testEquals()
{
String q="Yest";
Assert.
assertEquals("should
not equal to a string object",false,p.equals(q));
}
@Test
public void testGetPrice()
{
Assert.
assertEquals(32.45,p.getPrice(),0.01);
}
ShoppingCartTest.java:
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ShoppingCartTest {
private Product book1;
public ShoppingCartTest(){
}
@Before
public void setUp() throws Exception {
cart = new ShoppingCart();
book1 = new Product("Pragmatic Unit Testing", 29.95);
cart.addItem(book1);
}
public void tearDown() throws Exception {
}
@Test
public void testEmpty() {
cart.empty();
Assert.
assertEquals(0,
cart.getItemCount());
}
public void testAddItem() {
cart.addItem(book2);
Assert.
assertEquals(expectedBalance,
cart.getbalance(), 0.0);
}
//抛出异常
@Test
public void testRemoveItem() throws ProductNotFoundException {
Assert.
assertEquals(0,
cart.getItemCount());
}
public void testRemoveItemNotInCart() {//需要捕捉异常
try {
cart.removeItem(book3);
Assert.
assertTrue(true);
}
}
}
下面是测试套件的类:TestSuite.java(JUnit4)
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ShoppingCartTest.class,ProductTest.class})
另外为了在Suite添加一个测试方法,我们可以采用下面的一个办法:AllTests.java:
import junit.framework.TestSuite;
public class AllTests {
TestSuite suite = new TestSuite("Test for default package");
suite.addTest(new
ProductTest("testGetTitle"));//注意此时ProductTest必须要继承TestCase
return suite;
}
作为对比,我们再看一下JUnit3
写的测试类:
productTest.java:
import junit.framework.TestCase;
public class productTest extends TestCase {
private product p;
public productTest() {
}
public void setUp()
{
p=new product("a book",32.45);
}
public void tearDown()
{
}
public void test
GetTitle()
{
assertEquals
("a
book",p.getTitle());
}
public void test
SameRefrence()
{
//product q=new product("a sheet",12.56);
product q=p;
assertSame
("not equale
object",p,q);
}
public void test
Equales()
{
String q="Yest";
assertEquals
("should
not equal to a string object",false,p.equals(q));
}
}
ShoppingCartTest
.java:
public class ShoppingCartTest extends TestCase {
private ShoppingCart cart;
private product book1;
public ShoppingCartTest(){
public ShoppingCartTest(String method){
super(method);
}
/**
* 建立测试 fixture.
* 每个test函数运行之前都执行.
*/
protected void setUp() {
}
* 销毁fixture.
* 每个测试函数执行之后都运行
*/
protected void tearDown() {
// release objects under test here, as necessary
}
public void test
Empty() {
}
public void test
AddItem() {
cart.addItem(book2);
assertEquals
(expectedBalance,
cart.getbalance(), 0.0);
}
public void test
RemoveItem() throws productNotFoundException {
}
public void test
RemoveItemNotInCart() {
cart.removeItem(book3);
assertTrue
(true);
}
}
public static Test suite(){
TestSuite suite=new TestSuite();
suite.addTest(new ShoppingCartTest("testRemoveItem"));
suite.addTest(new ShoppingCartTest("testRemoveItemNotInCart"));
return suite;
}
}
下面这个是测试Suite的测试类:TestClassComposite
.java(JUnit3):
public static Test suite() { //注意suite的大小写
TestSuite suite = new TestSuite();
suite.addTestSuite(productTest.class);
suite.addTest(ShoppingCartTest.suite());
return suite;
}
}
通过代码,我们可以清楚的看到JUnit4和
JUnit2在测试套件时的区别,JUnit4在测试套件时,必须构造一个空类,而且使用Annotation的形式,即
@RunWith(Suite.class)
@Suite.SuiteClasses({ShoppingCartTest.class,ProductTest.class}),而JUuni3则
是普通的直接用函数调用,添加Suite。
发表评论
-
Java NIO与IO的详细区别(通俗篇)
2014-01-02 10:51 69252内核空间、用户空间 ... -
Java EE 单元测试
2013-09-12 10:56 4700觉得测试 Java EE 应用程序太困难、不方便或者太复杂? ... -
JUnit 测试建议
2013-07-22 09:04 3432以下是对JUnit实践的一 ... -
老生常谈:面试算法有必要吗?
2013-04-01 09:55 32527声明:虽然发表在愚人节,但是文章内容很严肃。 前几天在知 ... -
一步步开发 Spring MVC 应用
2013-02-23 10:53 10274Spring MVC 框架 Spring 框 ... -
「译」Java集合框架系列教程四:Set接口
2013-01-18 13:20 2495原文:The Set Interface 译文:Java集 ... -
「译」Java集合框架系列教程三:Collection接口
2013-01-18 13:16 2322原文:The Collection Interface 译 ... -
「译」Java集合框架系列教程二:集合接口
2013-01-18 13:13 1969原文:http://docs.oracle.c ... -
Java集合框架系列教程一:集合框架简介
2013-01-18 12:47 2986原文:http://docs.oracle.com/jav ... -
Java泛型-类型擦除
2012-12-05 15:48 14541一、概述 Java泛型在使用过程有诸多的 ... -
千万不要把 bool 当成函数参数
2012-11-16 08:33 12388我们有很多 Coding Style 或 代码规范。 但这一条 ... -
网站建设中关于eclipse启动参数的优化
2012-08-23 09:23 2458在网站建设中,经常用 ... -
Eclipse程序员要掌握的常用快捷键
2012-08-22 09:33 10006判断一个人的编程水平,就看他用键盘多,还是鼠标多。用键 ... -
java中equals和==的区别
2012-08-15 16:31 1991值类型是存储在内存中的堆栈(简称栈),而引用类型的变量在 ... -
名词王国里的死刑(翻译) - A Story of Hello World
2012-07-23 10:43 11754翻译自Steve Yegge的大 ... -
好代码是廉价的代码
2012-07-19 08:03 3252长久以来我一直主张:好代码是廉价的代码。 当我跟做开发 ... -
只要一个返回语句
2012-05-18 13:18 1787别再这样写了: publ ... -
java编程的78条黄金法则
2012-05-16 12:57 2316创建和销毁对象 1、考虑用静态工厂方法(返回类的实例的 ... -
for 循环为何可恨?
2012-05-15 12:44 1573Java的闭包(Closure)特征最近成为了一个热门话 ... -
Java 8 新功能
2012-05-02 08:48 1851我们已经急不可待想知道Java 8可用的新功能,其中大部 ...
相关推荐
JUnit4
junit3.8 和junit4 api
java junit4 jar完整包,junit可用来项目的单元测试。
运行junit4所需jar包,包括junit-4.11.jar与 hamcrest-core-1.3.jar
junit4学习文档,有实例,非常详细,不懂的可以参考
这是一个junit4测试的jar包,copy进去就可用
junit4教程(《Junit4初探》)
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,包含依赖包:junit-jupiter-5.5.1.jar,junit-jupiter-engine-5.5.1.jar,junit-...JUnit Vintage提供了一个在平台上运行JUnit 3和JUnit 4的TestEngine 。
JUnit4基础文档
此文档 详细介绍了JUnit4 和 3的主要区别,进行了实例详解!可以作为面试备用资料!
因 jdk5 中的新特性,JUnit4 也因此有了很大的改变。下面详细介绍 JUnit4 的使用方法,只是举个例子说明junit4 的使用
赠送jar包:powermock-module-junit4-2.0.9.jar; 赠送原API文档:powermock-module-junit4-2.0.9-javadoc.jar; 赠送源代码:powermock-module-junit4-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-...
JUnit4概述,在Eclipse中使用JUnit4进行单元测试(初级篇、中级篇、高级篇)
idea junitidea junitidea junitidea junitidea junitidea junit
Junit4使用方法
junit4的jar包,使用annotation进行junit测试!
单元测试Junit4 用于java测试开发
这个是junit4 的完整源代码,其中解压以后的文件中找到javadoc那个是文档。
junit4 jar包 Java单元测试框架绿色免费版.zip
Junit4教程.pdf