JUnit Cookbook
Kent Beck, Erich Gamma
Here is a short cookbook showing you the steps you can follow in writing and organizing your own tests using JUnit.
Simple Test Case
How do you write testing code?
The simplest way is as an expression in a debugger. You can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects. You can also write test expressions as statements which print to the standard output stream. Both styles of tests are limited because they require human judgment to analyze their results. Also, they don't compose nicely- you can only execute one debug expression at a time and a program with too many print statements causes the dreaded "Scroll Blindness".
JUnit tests do not require human judgment to interpret, and it is easy to run many of them at the same time. When you need to test something, here is what you do:
- Create a subclass of TestCase.
- Override the method runTest()
- When you want to check a value, call assertTrue() and pass a boolean that is true if the test succeeds
For example, to test that the sum of two Moneys with the same currency contains a value which is the sum of the values of the two Moneys, write:
public void testSimpleAdd() {
Money m12CHF= new Money(12, "CHF");
Money m14CHF= new Money(14, "CHF");
Money expected= new Money(26, "CHF");
Money result= m12CHF.add(m14CHF);
assertTrue(expected.equals(result));
}
If you want to write a test similar to one you have already written, write a Fixture instead. When you want to run more than one test, create a Suite.
Fixture
What if you have two or more tests that operate on the same or similar sets of objects?
Tests need to run against the background of a known set of objects. This set of objects is called a test fixture. When you are writing tests you will often find that you spend more time writing the code to set up the fixture than you do in actually testing values.
To some extent, you can make writing the fixture code easier by paying careful attention to the constructors you write. However, a much bigger savings comes from sharing fixture code. Often, you will be able to use the same fixture for several different tests. Each case will send slightly different messages or parameters to the fixture and will check for different results.
When you have a common fixture, here is what you do:
- Create a subclass of TestCase
- Add an instance variable for each part of the fixture
- Override setUp() to initialize the variables
- Override tearDown() to release any permanent resources you allocated in setUp
For example, to write several test cases that want to work with different combinations of 12 Swiss Francs, 14 Swiss Francs, and 28 US Dollars, first create a fixture:
public class MoneyTest extends TestCase {
private Money f12CHF;
private Money f14CHF;
private Money f28USD;
protected void setUp() {
f12CHF= new Money(12, "CHF");
f14CHF= new Money(14, "CHF");
f28USD= new Money(28, "USD");
}
}
Once you have the Fixture in place, you can write as many Test Cases as you'd like.
Test Case
How do you write and invoke an individual test case when you have a Fixture?
Writing a test case without a fixture is simple- override runTest in an anonymous subclass of TestCase. You write test cases for a Fixture the same way, by making a subclass of TestCase for your set up code and then making anonymous subclasses for the individual test cases. However, after a few such tests you would notice that a large percentage of your lines of code are sacrificed to syntax.
JUnit provides a more concise way to write a test against a Fixture. Here is what you do:
- Write a public void method in the fixture class. By convention, the name of the method begins with "test".
For example, to test the addition of a Money and a MoneyBag, write:
public void testMoneyMoneyBag() {
// [12 CHF] + [14 CHF] + [28 USD] == {[26 CHF][28 USD]}
Money bag[]= { f26CHF, f28USD };
MoneyBag expected= new MoneyBag(bag);
assertEquals(expected, f12CHF.add(f28USD.add(f14CHF)));
}
Create an instance of of MoneyTest that will run this test case like this:
new MoneyTest("testMoneyMoneyBag")
When the test is run, the name of the test is used to look up the method to run.
Once you have several tests, organize them into a Suite.
Suite
How do you run several tests at once?
As soon as you have two tests, you'll want to run them together. You could run the tests one at a time yourself, but you would quickly grow tired of that. Instead, JUnit provides an object, TestSuite which runs any number of test cases together.
For example, to run a single test case, you execute:
TestResult result= (new MoneyTest("testMoneyMoneyBag")).run();
To create a suite of two test cases and run them together, execute:
TestSuite suite= new TestSuite();
suite.addTest(new MoneyTest("testMoneyEquals"));
suite.addTest(new MoneyTest("testSimpleAdd"));
TestResult result= suite.run();
Another way is to let JUnit extract a suite from a TestCase. To do so you pass the class of your TestCase to the TestSuite constructor.
TestSuite suite= new TestSuite(MoneyTest.class);
TestResult result= suite.run();
Use the manual way when you want a suite to only contain a subset of the test cases. Otherwise the automatic suite extraction is the preferred way. It avoids you having to update the suite creation code when you add a new test case.
TestSuites don't only have to contain TestCases. They contain any object that implements the Test interface. For example, you can create a TestSuite in your code and I can create one in mine, and we can run them together by creating a TestSuite that contains both:
TestSuite suite= new TestSuite();
suite.addTest(Kent.suite());
suite.addTest(Erich.suite());
TestResult result= suite.run();
|
相关推荐
在Eclipse中使用JUnit4进行单元测试是一种常见的Java开发实践,它可以帮助开发者确保代码的正确性和稳定性。单元测试是软件开发中的重要环节,通过编写针对代码各个独立模块的测试用例,可以验证代码功能是否按预期...
- **JUnit版本兼容性**:需要注意的是,不同的Eclipse版本可能对应的JUnit版本不同,因此在设置`junit.jar`和源代码路径时需确保与当前使用的Eclipse版本相匹配。 - **源代码附件的作用**:通过附加JUnit的源代码,...
在Eclipse中使用Junit,你需要创建一个新的JUnit测试类。这可以通过右键点击项目中的源代码文件夹,选择“New” -> “JUnit Test Case”来实现。然后根据提示选择要测试的类和方法,Eclipse会自动生成一个基本的测试...
### 软件测试之Eclipse与JUnit使用详解 #### 一、JUnit简介与安装 JUnit 是一个流行的单元测试框架,主要用于 Java 编程语言。它最初由 Erich Gamma 和 Kent Beck 创建,目的是帮助开发者编写可靠的软件代码。通过...
1. 创建Java工程:首先,我们需要在Eclipse中创建一个新的Java工程,这将是我们的测试项目的基础。 2. 编写待测试的代码:在工程中添加一个名为`example.Hello`的类,例如包含一个`abs()`方法,该方法用于计算传入...
【标题】:“junit_eclipse实例”是一个关于在Eclipse集成开发环境中...通过实际操作这个实例,开发者可以深入理解单元测试的重要性,提升代码质量,同时也熟悉Eclipse中的测试工具,为后续的开发工作打下坚实基础。
本教程将详细介绍如何在Eclipse中使用JUnit4进行最基础的单元测试,同时提供相关文档和源码供学习参考。 首先,我们需要在Eclipse中创建一个Java项目。打开Eclipse,点击“File” -> “New” -> “Java Project”,...
这里以Eclipse Helios (3.6) for Java EE为例,讲解如何在Eclipse中设置和使用JUnit。 首先,你需要将JUnit库添加到你的项目中。在Eclipse中,右键点击项目,选择“属性”,然后在左侧选择“Java Build Path”。在...
在IT领域,尤其是在软件开发过程中,单元测试是确保代码质量和稳定性的重要环节。本文将深入探讨如何在Eclipse...对于任何希望提升软件工程实践的团队而言,掌握在Eclipse中使用JUnit进行单元测试都是不可或缺的技能。
以上只是Eclipse使用的一部分基础内容,实际使用中还有更多高级特性和技巧等待探索。通过这个中文教程,你将能够逐步掌握Eclipse,提升开发效率。记得不断实践和学习,以充分利用这款强大的开发工具。
### 终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python 本文档将基于给定的信息——“终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python”来详细阐述如何搭建一套完整的自动化测试框架,...
2. **IDE集成**:大部分Java IDE如Eclipse、IntelliJ IDEA等,内置了对JUnit的支持,只需简单配置即可使用。 **三、基础用法** 1. **创建测试类**:测试类通常与被测试的类位于同一包下,以"Test"后缀命名。 2. **...
为了在项目中使用这个整合包,你需要在项目的构建配置文件(如Maven的pom.xml或Gradle的build.gradle)中添加对应的依赖。对于Maven,你可能需要添加以下代码段: ```xml <groupId>org.junit.jupiter ...
JUnit5是Java编程语言中最流行的单元测试框架之一,它的最新版本带来了许多改进和新特性,使得测试更加...通过了解和应用以上知识点,开发者可以有效地在Java项目中使用JUnit5进行单元测试,确保代码的质量和可靠性。
5. `images` 目录 - 可能存储了插件UI中使用的图标和其他图像资源。 6. `web` 目录 - 通常用于存放Web相关的资源,可能是插件在Eclipse Marketplace上的网页内容。 使用这个插件,开发者可以快速构建测试基础设施,...
P2是Eclipse中的软件仓库和更新管理基础设施,它允许用户安装、更新和管理Eclipse插件及功能集。在这个特定的“update-site-eclipse-junit-server-result”中,用户可以找到并下载那些针对Junit测试结果展示和解析的...
在实际项目中,Junit4.5经常与其他工具结合使用,如Mockito用于模拟对象,提供隔离的测试环境;或者与持续集成服务器如Jenkins配合,自动化测试流程,保证代码质量。 总之,Junit4.5作为一个强大的单元测试框架,极...
总的来说,这个压缩包提供了学习JUnit4和进行单元测试的实战案例,对于理解如何在Eclipse中使用JUnit进行代码验证和调试是非常有价值的。通过分析和运行这些源码,学习者可以掌握单元测试的基本概念,了解如何编写...
Eclipse是一款强大的集成开发环境(IDE),尤其在Java开发领域中占据重要地位。随着技术的发展,各种IDE如雨后春笋般涌现,如JBuilder、Visual Age for Java、JDeveloper、Forte for Java、Visual Cafe等,以及开源...