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.
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.
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
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.
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.
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.
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.
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.
To summarize the new JUnit style:
- It Requires JDK 5 to run.
- Test classes do not have to extend from junit.framework.TestCase.
- Test methods do not have to be prefixed with ‘test’.
- There is no difference between the old assert methods and the new assert methods.
- Use @Test annotations to mark a method as a test case.
- @Before and @After annotations take care of set up and tear down.
- @BeforeClass and @AfterClass annotations take care of one time set up and one time tear down.
- @Test annotations can take a parameter for timeout. Test fails if the test takes more time to execute.
- @Test annotations can take a parameter that declares the type of exception to be thrown.
- JUnit4Adapter enables running the new JUnit4 tests using the old JUnit runners.
- Old JUnit tests can be run in the new JUnit4 runner.
相关推荐
JUnit 4.0 是一个广泛使用的Java编程语言的单元测试框架。它极大地简化了测试代码的编写,并提供了丰富的断言库来验证代码的行为。在Java开发中,单元测试是确保代码质量、可维护性和可扩展性的重要环节。JUnit 4.0 ...
10. **可插拔的测试运行器**:JUnit 4.0支持多种测试运行器,如命令行运行器、IDE集成的运行器等,允许用户根据项目需求选择合适的运行方式。 总的来说,`junit4.0.jar`是Java开发中不可或缺的工具,它提供了一套...
Junit 4.0 是Java开发领域中一个重要的里程碑,它是JUnit系列的第四个主要版本,专注于提升测试的灵活性和可扩展性。JUnit是Java语言中最广泛使用的单元测试框架,它使得开发者能够编写易于理解、运行快速且可靠的...
JUnit 4.0 是一个广泛使用的Java编程语言的单元测试框架。这个压缩包"junit4.0.zip"包含了JUnit 4.0的所有组件和必要的库文件,使得开发者能够高效地编写和运行他们的测试用例,确保代码的质量和可靠性。 在Java...
JUnit4.0是Java开发中广泛使用的单元测试框架,它为编写可重复执行的、可靠的测试提供了强大支持。本主题将深入探讨JUnit4的新特性,尤其是如何“放弃旧的断言,使用hamcrest断言”,以及如何有效地运行多个测试。 ...
《Junit 4.0 学习笔记》 JUnit 是一个广泛使用的Java编程语言的单元测试框架,尤其在敏捷开发和持续集成环境中扮演着至关重要的角色。本文将深入探讨JUnit 4.0版本的主要特性、核心概念以及如何有效利用它进行单元...
junit-4.0.jar
《深入理解Hibernate 4.2.4与JUnit 4.0》 在Java开发领域,Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了数据库操作,而JUnit则是进行单元测试的重要工具。本教程将围绕Hibernate 4.2.4版本与...
在给定的标题和描述中,我们关注的是如何使用Hibernate来连接PostgreSQL数据库,并且提到了一些关键的库文件,如hibernate-junit4.0、postgresql-8.4-jdbc等。 首先,让我们深入了解每个组件: 1. **Hibernate**:...
JUnit 4.0引入了许多改进,比如注解、参数化测试和测试套件等,使得测试代码更加简洁和易读。在Selenium项目中,你可以为每个功能或页面创建一个JUnit测试类,每个测试方法对应一个具体的测试场景。 在使用这些jar...
"JUnit in Action 3rd Edition" JUnit是一种流行的Java单元测试框架,由Kent Beck和Eric Gamma于1997年创立。JUnit在软件测试领域中扮演着重要的角色,帮助开发者编写高质量的代码。下面是关于JUnit的重要知识点: ...
JUnit4 是一个广泛使用的Java编程语言的单元测试框架。它为开发者提供了一种方便的方式来编写和执行可重复的、自动化的测试用例,确保代码的质量和功能的正确性。JUnit4.zip 包含了不同版本的 JUnit jar 文件以及...
标题中的"Year_junit4_poetqqj_"可能是指一个关于使用JUnit 4.0进行年度测试的项目或教程,而"poetqqj"可能是个人名称、项目代号或者是某种编码规则的一部分,但在这里没有具体含义。描述中的“测试模块使用Junit ...
JUnit4视频教程及源码,由于文件较大,分割为JUnit4_01.zip,JUnit4_02.zip,JUnit4_03.zip三个压缩包并附文档,下载后解压便可观看视频,导入代码,直接在自己的机器上运行
《JUnit in Action》是一本专为Java开发人员深入理解单元测试框架JUnit而编写的经典书籍。本书详尽地探讨了如何有效地使用JUnit进行软件测试,包括基础概念、高级特性和最佳实践,旨在帮助读者提升软件质量,降低...
JUnit是由Erich Gamma和Kent Beck创建的Java单元测试框架。 junit/junit/4.0/junit-4.0.pom
《HL7映射验证器:基于JavaScript的DSL与JUnit4.0的集成应用》 HL7映射验证器是一款专为医疗信息系统设计的工具,它主要用于验证两个HL7(Health Level Seven)版本2.X消息之间的字段映射是否准确无误。在IT行业中...
JUnit in Action中文版JUnit in Action中文版JUnit in Action中文版JUnit in Action中文版
《JUnit in Action》和《JUnit Recipes》是两本关于Java单元测试的重要书籍,它们深入浅出地介绍了如何使用JUnit框架进行高效、可靠的测试。JUnit是一个流行的开源测试框架,广泛用于Java应用程序的单元测试,它提供...