JUnit简介

好久没更新blog了。。。今天来说说Junit。

link:

http://www.vogella.de/articles/JUnit/article.html

 

UT定义:

A unit test is a piece of code written by a developer that tests a specific functionality in the code which is tested. Unit tests can ensure that functionality is working and can be used to validate that this functionality still works after code changes.

对于最后这点真是太有感触了。现在的UT + ITF 的形式,让任何一点代码改动都会被反映出来。所以UT + ITF对保证代码质量太有用了。

原则上每个类都应该有自己的UT,且每个测试类中应该包含被测试类中的主要方法。这里有一个UT的代码覆盖率的问题,即UT覆盖了被测试类中多少代码。一个工具Cobertura,能在报表中反映出所有UT的覆盖率,非常强大~~所以写UT的时候偷懒,该测试的方法没有测试也是不可以的~~~~

 

刚才guru还指点了我一点:

由于UT之间应该是完全独立的。所以如果你发现一个类的UT很难写,需要准备很多东西,在一定程度上说明代码之间的耦合度过高,代码写的不够好。

同时,在测试驱动的开发中,由于首先考虑了测试,则能降低代码之间的耦合度。这是测试驱动开始的另一个好处。

 

放置UT代码的地方也有讲究。通常是放置在以下src/test/java目录中:

 

JUnit

使用和原代码不同的目录目的是打jar包时不应该包含UT代码。同时,UT应和被测试类处于同一个package中,这样在写测试时将不用import被测试类。

这里再8一句,点击当前项目,右键->new->Source Folder 就可以新建一个和上面一样的,带小package标的文件夹的图标了~

 

言归正传,我们来看JUnit的具体内容。

在JUnit中实现UT主要包括两部分:

Annotate a method with @org.JUnit.Test

Use a method provides by JUnit to check the expected result of the code execution versus the actual result

既可以实现一个单独的JUnit Test Case, 也可以实现JUnit Test Suite。执行test suite会执行包含的全部test case。As below:

 

最重要的是记住以下这些annotation

Table 1. Annotations

AnnotationDescription
@Test public void method()Annotation @Test identifies that this method is a test method.
@Before public void method()Will perform the method() before each test. This method can prepare the test environment, e.g. read input data, initialize the class)
@After public void method()Test method must start with test
@BeforeClass public void method()Will perform the method before the start of all tests. This can be used to perform time intensive activities for example be used to connect to a database
@AfterClass public void method()Will perform the method after all tests have finished. This can be used to perform clean-up activities for example be used to disconnect to a database
@IgnoreWill ignore the test method, e.g. useful if the underlying code has been changed and the test has not yet been adapted or if the runtime of this test is just to long to be included.
@Test(expected=IllegalArgumentException.class)Tests if the method throws the named exception
@Test(timeout=100)Fails if the method takes longer then 100 milliseconds

以及如下常用断言

Table 2. Test methods

StatementDescription
fail(String)Let the method fail, might be usable to check that a certain part of the code is not reached.
assertTrue(true);True
assertsEquals([String message], expected, actual)Test if the values are the same. Note: for arrays the reference is checked not the content of the arrays
assertsEquals([String message], expected, actual, tolerance)Usage for float and double; the tolerance are the number of decimals which must be the same
assertNull([message], object)Checks if the object is null
assertNotNull([message], object)Check if the object is not null
assertSame([String], expected, actual)Check if both variables refer to the same object
assertNotSame([String], expected, actual)Check that both variables refer not to the same object
assertTrue([message], boolean condition)Check if the boolean condition is true.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值