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:
- 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 Platform支持多种测试框架,而JUnit Jupiter是新测试API的核心,JUnit Vintage则允许在JUnit5中运行JUnit4的测试。 2. **注解增强**:JUnit5扩展了注解,如`@Test`、`@BeforeAll`、`@BeforeEach`、`@After...
JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了强大的工具来编写、组织和执行单元测试。JUnit4引入了许多改进和新特性,极大地提升了测试的灵活性和效率。下面将详细介绍JUnit4的关键概念、...
JUnit4 是一个广泛使用的Java编程语言的单元测试框架。它为开发者提供了一种方便的方式来编写和执行可重复的测试,确保代码的质量和稳定性。这个压缩包 "JUnit4.zip" 包含了JUnit 4的不同版本,从4.7到4.11,这将有...
这个名为"junit5-samples"的压缩包包含了一个使用JUnit 5的示例应用程序集合,旨在帮助开发者更好地理解和运用JUnit 5的各种特性。 在JUnit 5中,最大的变化是它被分为三个主要模块:JUnit Platform、JUnit Jupiter...
JUnit 5 是一个用于Java语言的单元测试框架,它由...对于习惯于JUnit 4的用户,JUnit 5带来了许多新特性和变化,这需要一定的时间去适应和掌握。不过,随着社区对新版本的熟悉,JUnit 5有望成为Java测试领域的新标准。
JUnit4在JUnit3的基础上进行了大量改进,特别是通过使用Java 5的注解特性来简化测试用例的编写。 1. **无需继承TestCase**:JUnit4不再要求测试类必须继承`TestCase`类。 2. **测试方法标识**:使用`@Test`注解来...
通过使用Java 5注解,JUnit4不仅简化了测试代码的编写,还引入了许多强大的新特性,如参数化测试、异常测试和计时测试等。这些特性使得测试变得更加高效、灵活且易于维护。对于希望提高软件质量的Java开发者而言,...
虽然较新版本如JUnit 4和JUnit 5功能更强大,但3.8.1版仍被一些老项目或对新特性需求不高的项目所使用。 4. **核心概念**: - **测试注解**:在JUnit 3中,测试方法并没有像后来的版本那样通过注解标识,而是通过...
JUnit是Java编程语言中最广泛使用的单元测试框架,而Junit4是其一个重要的版本,引入了许多新特性,极大地提升了测试的效率和可读性。 ### Junit4核心概念 1. **测试注解**:Junit4的核心变化之一是引入了注解...
《深入解析JUnit 5:基于junit5-r5.3.1.zip的源码分析》 JUnit 5作为Java领域中最流行的单元测试框架之一,它的每次更新都备受开发者关注。在junit5-r5.3.1.zip这个压缩包中,我们得到了JUnit 5的源码,这为我们...
JUnit4的主要目标是利用Java 5的注解(Annotation)特性来简化测试用例的编写流程。这不仅提升了测试代码的可读性,也降低了编写测试的门槛。 #### 二、注解(Annotation)概念解析 注解在Java 5中被引入作为一种...
随着版本的不断更新,JUnit4带来了革命性的改进,其中最显著的特点是充分利用了Java5的Annotation特性,极大地简化了测试用例的编写过程。本文旨在通过深入解析JUnit4的关键特性,帮助读者快速掌握这一高效测试工具...
JUnit 5支持从Java 8到最新的Java版本,这意味着开发者可以利用最新的Java特性来编写更加高效、简洁的测试代码。 **1.3 获取帮助** 官方文档和社区是获取帮助的重要资源。官方文档提供了详尽的指南和API文档,而...