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