JUnit @ Eclipse 2
2004-12-08 21:22
591 查看
Eclipse ver 3可以直接使用JUnit Winzard创建,但在ver 2中使用前需要配置,在《Eclipse in Action》中有详细介绍。以下是其原文,加上了简单引导!
一、首先创建一个工程或选择(创建)一个工作集
A sample application and working sets
二、创建class variables: JUNIT 加入工程文件中
First you define the class variables using the Package Explorer as follows:
1 Right-click on the project name and select Properties from the context
menu.
2 In the Properties dialog that appears, select Java Build Path in the right
pane and select the Libraries tab.
3 On this page, click the Add Variable button.
4 On the next page, click New. Enter JUNIT for the variable name and click
the File button to browse for the JUnit JAR file under the Eclipse plugins
directory; this may be, for example, c:/eclipse/plugins/org.junit_3.8.1/
junit.jar.
5 Click Open to select the JAR from the file dialog box, and then click OK
to accept the new variable.
6 Next you’ll add a variable for the source JAR for JUnit, in case you need
it for debugging. Click New again, and this time enter JUNIT_SRC as the
name. Click File and locate the junitsrc.zip under the JDT source directory;
e.g. C:/eclipse/plugins/org.eclipse.jdt.source_2.1.0/src/org.junit_3.8.1/
junitsrc.zip.
7 Click OK to return to the New Variable Classpath Entry box.
三、创建class variables:JUNIT_SRC与JUNIT关联,(原由:调试时需要源代码)
Now you’ll add the JUNIT variable to your classpath and associate the source JAR with it, using the JUNIT_SRC variable:
1 Click on the JUNIT classpath variable and click OK.
2 Make sure you are on the Java Build Path page in the Properties dialog
box, and click the plus sign next to the JUNIT entry. You will see that
there is no Javadoc and no source attached.
3 Double-click on Source Attachment and enter the variable name JUNIT_
SRC. Click OK and verify that the source JAR (for example, c:/eclipse/
plugins/org.junit_3.8.1/src.jar) is now attached.
4 Click OK to save the classpath changes and dismiss the Properties dialog
box.
Note that a JUNIT library is now listed in the Package Explorer. If you open the library (by clicking the + sign), you can explore the contents of the library.
四、使用JUnit Winzard创建test case classes
The easiest way to create test case classes is to use the JUnit wizard:
1 Right-click on the file for which you want to create test cases—FilePersistenceServices—and select New→Other from the context menu.
2 Notice that in the New dialog box, you can expand the Java selection on
the left by clicking the plus sign. Doing so reveals a selection for JUnit.
3 Select JUnit on the left to present the choices TestCase and TestSuite on
the right.
4 Select TestCase (see figure 3.1). Click Next.
5 In the box that follows, accept the default values provided for the folder,
package, test case, test class, and superclass. Later, especially for larger
projects, you may consider putting tests in their own package, but keeping
unit tests in the same package as the code they test has the advantage
of giving them access to methods that have package access.
6 In addition to the default test entries, click the options to create method
stubs for setUp() and tearDown() (see figure 3.2). Click Next.
7 In the next dialog box, you are presented with the option to create method
stubs to test each of the methods in the FilePersistenceServices class
and its superclass Object. Check the boxes for the FilePersistenceServices
read() and write() methods (see figure 3.3). (If you don’t see the
read() and write() methods, you probably didn’t save the FilePersistenceServices class after adding them. Click Cancel and try again.)
8 Click Finish.
You’re finally ready to add some tests. JUnit’s primary tools for testing are a variety of overloaded assert methods(public void assertXXXX) for testing an expression or pair of expressions.These include the following:
■ assertEquals( x, y)—Test passes if x and y are equal. x and y can be primitives or any type that has an appropriate equals() method.
■ assertFalse( b)—Test passes if boolean value b is false.
■ assertTrue( b)—Test passes if boolean value b is true.
■ assertNull( o)—Test passes if object o is null.
■ assertNotNull( o)—Test passes if object o is not null.
■ assertSame( ox, oy)—Test passes if ox and oy refer to the same object.
■ assertNotSame( ox, oy)—Test passes if ox and oy do not refer to the same
object.
五、运行
select Run→Run As→JUnit Test.
六、在test case classes创建新方法,借助Quick Fix tool在对应类中生成方法,实践测试先行!
1 Click on one of the light bulbs.
2 Double-click on the suggested fix:create yourMethodName
相关文章推荐
- DirectX.Capture
- 越过调试这道槛——ASP.NET无法调试问题剖析
- 一些好用的Eclipse 3.0插件
- Ant应用(1)
- 关于SWT/JFACE
- servlet生命周期
- log4j的配制及测试程序
- [转载]jbx 乱码问题的解决大全
- 关于数据源连接提供者和数据源连接
- Industry Standards
- What do I Need to Get Started?
- log4j使用示例
- Eclipse3.1M2部分支持JDK5语法
- Pragmatic Unit Testing in Java with JUnit 书评
- python速查笔记(1) 类型与函数
- ◆[转]Java面试陷阱第二波
- 天天学英语之1115
- 一个投票程序
- Cloning a Windows Database
- 怎样在一个一般窗口或是Dialog上面使用分割窗口.