JUnit单元测试笔记

#01 JUnit简介

1.在项目工程中的Library,add 一个JUnit的Jar包,按需要添加JUnit 3 或 JUnit 4(分为被测试类与测试类较佳)。

2.单元测试是由程序员完成的。

3.Java 5 之前的版本只能 用JUnit 4前的版本(因为JUnit 4用到Java 5的新特性----Annotation注解)。

4.JUnit 4只是工具。

#02  setUp(),tearDown()及相关断言方法。

1.JUnit3 利用反射,JUnit4利用了注解。

2.JUnit3中setUp()初始方法,而tearDown()销毁方法;而JUnit4利用@Before和@After来类似的功能。

3.JUnit3中的测试方法必须是以test开头。

4.单元测试是由程序员编写的,测试被测试代码的某一个很小的、特定的功能区域的代码。它可以用来确保在代码或者程序运行的环境中发生变化后,已经存在的功能还是能够执行的。

5.JUnit3中,每一个类都继承了一个叫做TestCase的类,而TestCase的父类是Assert类;JUnit 4,每一个可以不继承任何的类,但是仍然可以直接使用import static org.junit.assert.*;在JUnit3中,所有的测试类必须都是TestCase的子类;JUnit4中,测试类可以是一个普通类,也可以去继承一个类或者实现一个接口;要实现测试,只需要在要测试的方法之前加@Test 注释。

6.相关断言方法:

 

assertSame()与assertEquals()并不完全相同;same== 相同,equals==相等

#03 @AfterClass 与@BeforeClass用法

1.@AfterClass,@BeforeClass与@After,@Before,跟Java中的 静态块非静态块 功能相类似。

2.@AfterClass,@BeforeClass用例:数据库的连接(只用于一次)。

#04 @Ignore,@Test(expected = ***.class),@Test(timeout = ^[1-9][0-9]*(millisecond))

#05 JUnit中的Failure,Error与Java 中的Exception,Error 的区别

 

JUnit中

Failure指的是由于预期的结果与实际运行的测试的结果不同而导致的實際運行單元的結果不同所導致,例如当使用assertEquals()或其它assertXXX()方法断言失败时,就会报出Failure,如果发现Faulure,你就要去检查你的测试方法或者是被测试方法中编写的逻辑是否有误。

Error指的是编写程序时没有考虑到的问题。在执行测试的断言之前,程序就因为某种类型的意外而停止,比喻说我们在操作数组的时候,因为存取超出索引会引发ArrayIndexOutOfBoundsException,这个时候程序就会报出Error,程序将无法运行下去,提前结束,这个时候你要检查被测试方法中是不是有欠缺考虑到地方。

Java中

Exception: The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

Error: An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值