分享
 
 
 

JUnit @ Eclipse 2

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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.1junit.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.1junitsrc.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:\eclipseplugins\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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有