`

junit4的变化:注意其中使用了java5的新特性

 
阅读更多
JUnit 4.0 in 10 minutes Print E-mail

Gunjan Doshi
Instrumental Services Inc.

Download the PDF Version of this article.

Abstract: JUnit needs no introduction. Originally written by Kent Beck and Erich Gamma, the software is the preferred tool of choice for developer testing. Now, the team of Kent Beck and Erich Gamma is back again with a new version of JUnit – 4.0. This quick reference guide is for programmers and testers looking to migrate to JUnit 4.0. If you have a flight to catch or do not want to spend 10 minutes going through the guide, just jump to the summary section and you will learn enough.

For the purpose of this article, I will call JUnit 3.8.1 and its predecessors as the old JUnit and JUnit 4.0 as the new JUnit.

Table of contents:

This guide contains the following sections:

· Old JUnit revisited

· Cut the chase to JUnit 4.0

· Run the tests

· Set up and tear down

· One-time set up and tear down

· Expecting exceptions

· Other Annotations

o Ignoring a test

o Timing out a test

· Summary

Old JUnit revisited

Using the old JUnit, let us write a test, which verifies the availability of a book in the library.

To summarize the steps:

· We extend from junit.framework.TestCase.

· We name the test methods with a prefix of ‘test’.

· We validate conditions using one of the several assert methods.

Cut the chase to JUnit 4.0

Let us write the same test using JUnit 4.0.

When I upgrade to a new version I look for tasks, I do not have to do anymore. Here is the same code with notes telling us what not to do anymore.

To summarize:

· We do not extend from junit.framework.TestCase.

· We do not prefix the test method with ‘test’.

Next, I look for new tasks I must always do. The diagram below summarizes what we must do according to the new JUnit standards:

To summarize:

· Use a normal class and not extend from junit.framework.TestCase.

· Use the Test annotation to mark a method as a test method. To use the Test annotation, we need to import org.junit.Test

· Use one of the assert methods. There is no difference between the old assert methods and the new assert methods. An easy way to use the assert method is to do a static import as shown by point 2 in the code above.

· Run the test using JUnit4TestAdapter. If you want to learn more about JUnit4TestAdapter, keep reading ahead.

Run the tests

Unfortunately, our favorite development environments are still unaware of JUnit 4. JUnit4Adapter enables compatibility with the old runners so that the new JUnit 4 tests can be run with the old runners. The suite method in the diagram above illustrates the use of JUnit4Adapter.

Alternatively, you can use the JUnitCore class in the org.junit.runner package. JUnit 4 runner can also run tests written using the old JUnit. To run the tests using the JUnitCore class via the command line, type:

java org.junit.runner.JUnitCore LibraryTest

Set up and tear down

The new JUnit provides two new annotations for set up and tear down:

· @Before: Method annotated with @Before executes before every test.

· @After: Method annotated with @After executes after every test.

Here is the code that demonstrates the use of @Before and @After:

Two features of @Before and @After annotations that are helpful to learn:

· You can have any number of @Before and @After as you need.

· It is possible to inherit the @Before and @After methods. New JUnit executes @Before methods in superclass before the inherited @Before methods. @After methods in subclasses are executed before the inherited @After methods.

One-time set up and tear down

The new JUnit4 provides @BeforeClass and @AfterClass annotations for one-time set up and tear down. This is similar to the TestSetup class in the old junit.extensions package, which ran setup code once before all the tests and cleanup code once after all the tests.

Here is the code that demonstrates @BeforeClass and @AfterClass:

Unlike @Before and @After annotations, only one set of @BeforeClass and @AfterClass annotations are allowed.

Expecting exceptions

The new JUnit makes checking for exceptions very easy. The @Test annotation takes a parameter, which declares the type of Exception that should be thrown. The code below demonstrates this:

In the code above, bookNotAvailableInLibrary is a test, which passes only if BookNotAvailableException is thrown. The test fails if no exception is thrown. Test also fails if a different exception is thrown.

Other Annotations

Ignoring a test

The @Ignore annotation tells the runner to ignore the test and report that it was not run. You can pass in a string as a parameter to @Ignore annotation that explains why the test was ignored. E.g. The new JUnit will not run a test method annotated with @Ignore(“Database is down”) but will only report it. The version of JUnit4Adapter, I used, did not work with @Ignore annotation. Kent Beck has informed me that the next version of JUnitAdapter will fix this problem.

Timing out a test

You can pass in a timeout parameter to the test annotation to specify the timeout period in milliseconds. If the test takes more, it fails. E.g. A method annotated with @Test (timeout=10) fails if it takes more than 10 milliseconds.

Finally, I would like to thank Kent Beck for taking the time to demonstrate and teach the new JUnit to me.

Summary

To summarize the new JUnit style:

  1. It Requires JDK 5 to run.
  2. Test classes do not have to extend from junit.framework.TestCase.
  3. Test methods do not have to be prefixed with ‘test’.
  4. There is no difference between the old assert methods and the new assert methods.
  5. Use @Test annotations to mark a method as a test case.
  6. @Before and @After annotations take care of set up and tear down.
  7. @BeforeClass and @AfterClass annotations take care of one time set up and one time tear down.
  8. @Test annotations can take a parameter for timeout. Test fails if the test takes more time to execute.
  9. @Test annotations can take a parameter that declares the type of exception to be thrown.
  10. JUnit4Adapter enables running the new JUnit4 tests using the old JUnit runners.
  11. Old JUnit tests can be run in the new JUnit4 runner.
分享到:
评论

相关推荐

    Spring_Framework_ API_5.0.5 (CHM格式)

    Spring5 的基准版本为8,因此它使用了 Java8 和9的许多新特性。例如: Spring 接口中的默认方法 基于 Java8 反射增强的内部代码改进 在框架代码中使用函数式编程 - lambda表达式 和 stream流 4. 响应式编程...

    【白雪红叶】JAVA学习技术栈梳理思维导图.xmind

    关于java程序员发展需要学习的路线整理集合 技术 应用技术 计算机基础知识 cpu mem disk net 线程,进程 第三方库 poi Jsoup zxing Gson 数据结构 树 栈 链表 队列 图 操作系统 linux 代码控制...

    MySQL 5.1官方简体中文参考手册

    1.6.1. MySQL 5.1的新特性 1.7. MySQL信息源 http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chapter/(第 1/24 页)2006-11-02 19:12:13 MySQL 5.1 Reference Manual 1.7.1. MySQL邮件列表 1.7.2. IRC(在线聊天...

    MySQL5.1参考手册官方简体中文版

    1.6.1. MySQL 5.1的新特性 1.7. MySQL信息源 1.7.1. MySQL邮件列表 1.7.2. IRC(在线聊天系统)上的MySQL社区支持 1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. ...

    mysql官方中文参考手册

    1.6.1. MySQL 5.1的新特性 1.7. MySQL信息源 1.7.1. MySQL邮件列表 1.7.2. IRC(在线聊天系统)上的MySQL社区支持 1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. ...

    MySQL 5.1参考手册

    1.6.1. MySQL 5.1的新特性 1.7. MySQL信息源 1.7.1. MySQL邮件列表 1.7.2. IRC(在线聊天系统)上的MySQL社区支持 1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. ...

    MYSQL中文手册

    1.6.1. MySQL 5.1的新特性 1.7. MySQL信息源 1.7.1. MySQL邮件列表 1.7.2. IRC(在线聊天系统)上的MySQL社区支持 1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 ...

    MySQL 5.1参考手册中文版

    1.6.1. MySQL 5.1的新特性 1.7. MySQL信息源 1.7.1. MySQL邮件列表 1.7.2. IRC(在线聊天系统)上的MySQL社区支持 1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 ...

    MySQL 5.1中文手冊

    1.6.1. MySQL 5.1的新特性 1.7. MySQL信息源 1.7.1. MySQL邮件列表 1.7.2. IRC(在线聊天系统)上的MySQL社区支持 1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. ...

    MySQL 5.1参考手册 (中文版)

    1.6.1. MySQL 5.1的新特性 1.7. MySQL信息源 1.7.1. MySQL邮件列表 1.7.2. IRC(在线聊天系统)上的MySQL社区支持 1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是什么 1.8.2. ...

    mysql5.1中文手册

    MySQL 5.1的新特性 1.7. MySQL信息源 1.7.1. MySQL邮件列表 1.7.2. IRC(在线聊天系统)上的MySQL社区支持 1.7.3. MySQL论坛上的MySQL社区支持 1.8. MySQL标准的兼容性 1.8.1. MySQL遵从的标准是...

    重构-改善既有代码的设计(中文版)

    Chapter 5:Toward a Catalog of Refactorings 重构目录   Format of the Refactorings 重构描述的格式   Finding References 寻找引用   How Mature Are These Refactorings? 这些重构的成熟度如何 Chapter ...

    重构:改善既有代码的设计(中文高清版)

    3.5 DivergentChange(发散式变化)79 3.6 ShotgunSurgery(霰弹式修改)80 3.7 FeatureEnvy(依恋情结)80 3.8 DataClumps(数据泥团)81 3.9 PrimitiveObsession(基本类型偏执)81 3.10 SwitchStatements(switch...

    重构_改善既有代码的设计.pdf

    3.5 DivergentChange(发散式变化)79 3.6 ShotgunSurgery(霰弹式修改)80 3.7 FeatureEnvy(依恋情结)80 3.8 DataClumps(数据泥团)81 3.9 PrimitiveObsession(基本类型偏执)81 3.10 SwitchStatements(switch...

    重构-改善既有代码的设计 中文版

    3.5 Divergent Change(发散式变化) 3.6 Shortgun Surgery(霰弹式修改) 3.7 Feature Envy(依恋情结) 3.8 Data Clumps(数据泥团) 3.9 Primitive Obsession(基本型别偏执) 3.10 Switch Statements(switch...

    重构-改善既有代码的设计

     3.5 DivergentChange(发散式变化)79  3.6 ShotgunSurgery(霰弹式修改)80  3.7 FeatureEnvy(依恋情结)80  3.8 DataClumps(数据泥团)81  3.9 PrimitiveObsession(基本类型偏执)81  3.10 ...

Global site tag (gtag.js) - Google Analytics