junit 简单介绍(入门)

当你需要编写更多的test case的时候,你可以创建更多的TestCase对象。当你需要一次执行多个TestCase对象的时候,你可以创建另一个叫做TestSuite的对象。为了执行TestSuite,你需要使用TestRunner。

TestCase + TestSuite + BaseTestRunner = TestResult

 

 Test runner:

JUnit发布包中包含了3个TestRunner类:一个用户文本控制台,一个用户Swing,甚至还有一个AWT。
并不存在TestRunner接口,这和JUnit框架中的其它元素不同。相反,JUnit附带的几个test runner都是继承自BaseTestRunner。如果你不管什么理由需要编写你自己的test runner,你也可以继承这个类。例如:Cactus框架就继承了BaseTestRunner来创建一个ServletTestRunner,可以从浏览器中运行JUnit测试。

 

用TestSuite来组合测试:

 若想要运行多个test case又会如何呢?如果想在多个test case中选一部分又怎样呢?应当如何来为test case分组呢?
看来,在TestCase和TestRunner之间,你还需要某种容器,用来把几个测试归在一起,并且把他们作为一个集合一起运行,但是,在使得多个test case运行得以简化的同时,你也不希望让运行单个test case变得复杂化。
JUnit对这个难题的回答是TestSuite。TestSuite被设计成可以运行一个或多个test case,test runner负责启动TestSuite,而要运行哪些test case则由TestSuite来决定。
若你没有提供自己的TestSuite,test runner会自动创建一个。
缺省的TestSuite会扫描你的测试类,找出所有以test开头的方法。缺省的TestSuite在内部为每个testXXX方法都创建一个TestSuite的实例,要调用的方法的名字会传递给TestCase的构造函数,这样每个实例就有一个独一无二的标识。

 

 

用TestResult来收集参数:
TestResult负责收集TestCase的执行结果。如果所有的测试都总是可以顺利的完成,那么为什么还要运行他们呢?所以,TestResult储存了所有测试的详细情况,是通过还是失败。
TestRunner使用TestResult来报告测试结果。如果TestResult集合中没有TestFailure对象,那么代码就是干净的,进度条就的绿色的;否则TestRunner就会报失败,并输出失败测试的数目和它们的stack trace。
你只需要知道他的存在就OK了。

 

 

用TestListener来观察结果:

JUnit框架提供了TestListener接口,以帮组对象访问 TestResult并创建有用的报告。TestRunner实现了 TestListener,很多特定的JUnit扩展和实现了TestListener。可以有任意数量的TestLintener向JUnit框架注册,这些TestListener可以根据TestResult提供的信息做它们需要做的任何事情。

 

 

用TestCase来工作:

 概括地说,JUnit的工作过程就是由TestRunner来运行包含一个或者多个TestCase(或者其他TestSuite)的TestSuite。但是在常规的工作中,你通常只和TestCase打交道。

 

 

junit_demo1:

创建一个普通的java工程,导JUNIT4的jar
编写基本类:
package cn.partner4java.helloworld;

import junit.framework.TestCase;

public class HelloWorld extends TestCase {

public void testHelloWorld(){
int i = 1/0;
}

}

这就是一个TestCase

 

 

有啥用呢?

 在outLine找到这个方法,点击右键,run as Junit test,就会弹出结果显示

还是不知道有啥用对吧?
创建:cn.partner4java.helloworld2.ArithmeticService
创建:cn.partner4java.helloworld2.ArithmeticServiceTest

 package cn.partner4java.helloworld2;

public class ArithmeticService {
 private int num1 = 0;
 private int num2 = 0;
 
 public ArithmeticService(int num1,int num2){
  this.num1 = num1;
  this.num2 = num2;
 }
 
 public int add(){
  return num1 + num2;
 }
 
 public int delete(){
  return num1 - num2;
 }
}


package cn.partner4java.helloworld2;

import junit.framework.TestCase;

public class ArithmeticServiceTest extends TestCase {
 private ArithmeticService arithmeticService;
 
 @Override
 protected void setUp() throws Exception {
  super.setUp();
  arithmeticService = new ArithmeticService(12,3);
 }
 
 public void testAdd(){
  System.out.println(arithmeticService.add());
 }
 
 public void testDelete(){
  System.out.println(arithmeticService.delete());
 }
 
}

 

 

不知道你看完这两个类,能否明白JUnit是干的什么的。(哦,是用来测试我的类的方法是否正确)

我们以前面同样的方法执行testAdd,你们说会打印什么结果呢?(如果你认真听我们前面讲的内容了,你就会知道答案)

 

 

 

 很抱歉,不是15,而是:
15
9
两个结果

接下类我们说TestSuite:
修改:ArithmeticServiceTest

执行testDelete,打印什么?

 

 

package cn.partner4java.helloworld3;

import junit.framework.TestCase;
import junit.framework.TestSuite;

public class ArithmeticServiceTest extends TestCase {
 private ArithmeticService arithmeticService;
 
 public ArithmeticServiceTest(String string) {
  super(string);
 }

 @Override
 protected void setUp() throws Exception {
  super.setUp();
  arithmeticService = new ArithmeticService(12,3);
 }
 
 public void testAdd(){
  System.out.println(arithmeticService.add());
 }
 
 public void testDelete(){
  System.out.println(arithmeticService.delete());
 }
 
 public static TestSuite suite(){
  TestSuite suite = new TestSuite();
  suite.addTest(new ArithmeticServiceTest("testAdd"));
  return suite;
 }
}

 

 

答案:15
明白为什么了吧?

当然也可以把TestSuite拿出来单独写:
如:cn.partner4java.helloworld4.ArithmeticServiceTestSuite
执行结果:
15
9

 

package cn.partner4java.helloworld4;

import cn.partner4java.helloworld3.ArithmeticServiceTest;
import junit.framework.Test;
import junit.framework.TestSuite;

public class ArithmeticServiceTestSuite extends TestSuite {
 public static Test suite() {
  TestSuite suite = new TestSuite(ArithmeticServiceTest.class);
  return suite;
 }
}

 

 

那么我们的TestRunner和TestResult,聪明的你会想到,不就是我们这里的测试结果界面和测试返回报告么。
在现实的编程中,我们也很少需要手工去写这两种类。

 

 

 多说一嘴:

 

 Junit4,其实一般是这么写的:
cn.partner4java.helloworld5.HelloWorld

JDK5以后的新特性,注解,你也可以自定义注解。

 

 

package cn.partner4java.helloworld5;


import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HelloWorld {

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
  System.out.println("setUpBeforeClass");
 }

 @AfterClass
 public static void tearDownAfterClass() throws Exception {
  System.out.println("tearDownAfterClass");
 }
 
 @Test
 public void testHelloWorld(){
  System.out.println("testHelloWorld");
 }

}

 

 

当然还有断言的配合使用,这里就先不说了

 

 

 

这是我以前整理培训的ppt的内容,可下载了看看,只供参考,只是一个入门培训,不过平时的使用已经够了

 http://download.csdn.net/source/3022235

 

 

 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值