Eclipse的Junit配置_第1页
Eclipse的Junit配置_第2页
Eclipse的Junit配置_第3页
Eclipse的Junit配置_第4页
Eclipse的Junit配置_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、叵区filter textJava+ General|+! Ant|+i Help+ Ins IdLl/Update+ Pluigini+ Run/DebugTeamGeneral E&ttingE for Java developuneBt:丸亡tion on double click in the Package Exploreri into thw: s«lectd, element!* Expssid th电 Selected elementWhwit opening: h Type Hi erarchy宀 Op an a new Type Hi erarchy Pe

2、rspective Show the Type Hiejrarehy View in the current perspectiveRefactoring Java CodeSaye all modified resourees automatically prior to refactoringSrctkJy Ust reduced srch mtnuRe st ere Defa-ultsApplyOKCanc&l图 1.1 Prefere nces 对话框1 Eclipse 中 Junit 的配置Eclipse IDE中集成了 JUnit组件,无须另行下载和安装,但是为了更好的使用

3、Junit(更多细节请参考 ),我们还要在Eclipse中进行一些设置。其中主要是就是类路径变量的设置1.1 仓U建变量 JUNIT 和 JUNIT_SRC在project的classpath中添加junit.jar。因此,建立一个新的 classpath变量JUNIT。选择 Window -> Preferences 菜单栏打开 Preferences对话框,如图 1.1所示。展开 Java 结点,选择 Build Path -> Classpath Variables 项,如图 1.2所示。* Preferencestype filter讶

4、GeneralAntHelpInstall/UpdatftJava+ AppearanceBuildFftthUser Librsari es+ Code Style+ Compiler+;牛 EditorInstalled JKEsJUnitFropsrties Files Pliag-in. Deelopment Rm/D 电 bdgTeamClasspath Variables3 *k classpath vari abl& can b色 added to < pFOj色亡七"g fl宜営占 path. It caii be .std Iq dafina th&#

5、171; local i oxi of a JAR filt that i sn t part of the wrkspacg. T扎es reserved clss path variables. JEE_IZB“ JRE詔SRC JBE_SB£KOOT are set int ernally d«p ending on the set ting;Defined ?1b5spath vari abl#s;&BCIIPSE_HOME - dAProp-am FilesVeclipse砾 JRELIE tr a s erved) - D: Progr am F ile

6、s.Java jrel - S9JBE_SEC (res.ervied) - (empty)JRE_SRCB3OT Grswerved) - (empty)J1TBITO1E = d: Fr ogr am FilesXeqli pse S.plugi ns Ver g.&MYECIJPSELIBffiME - D:P“gr訥 FilWEcIipse 乩 MYICUPSE_STRVTS_MDME - DAFrgr Fil嶺VlyEdLi*和 BMYECLIPSE/HREJT扎JOflE - D:P“荻则 Fil«WEdillOK I Cancel图 1.2 Classpath

7、Variables 窗口然后点击New按钮打开New Variable Entry菜单,如图1.3所示。输入新的变量名:JUNIT,配置路径为junit.jar。在 Eclipse3.0.2中,能够在/eclipse/plugins/org.junit_3.8.1/junit.jar 下找到 junit.jar。然后点击 OK,如图 1.4所示。图 1.3 New Variable Entry 对话框图1.4 创建JUNIT变量为了 debug的需要,也要添加JUnit包的源代码。您将在 /eclipse/plugi ns/org.eclipse.jdt.source_3.0.2/src 目录

8、中发现许多包的源代码的 JAR 文档。为JUnit源代码创建一个新的变量,按照上面创建 JUNTIT的步骤,但是 此变量名为JUNIT_SRC (通常在源代码变量前面添加前缀_SRC)并且将他连 接至U /eclipse/plugins/org.eclipse.jdt.source_3.0.2/src/org.junit_3.8.1/junitsrc.zip,女口图1.5所示图1.5 创建JUNIT_SRC变量单击0K命令按钮,这样两个环境变量就创建完成,我们可以看见JUNIT和JUNIT_SRC两个变量,如图1.6所示 Preferences|type filter l&i!寸+1+

9、CEasspath Variables(?«nerkLAntJklpInitall/Upiatt Jiva!+l-Appearan.deBuild FathCl asspath Var iUser Librar工色生Code StyleCompilerDebugEditorInstalled JKEsJUnitFropsrti as File:s Pliag-in Deelapment Rua/DbUng TeamA daWWp盘th V'iri abl色 t絆 b© added to 直 project S cla.££ pat_h. It C

10、STt t» vx»d to dtfiiii th« Iccati cn of a JAR fil» that i sn t part of the warkspacft. The reserved clue pAth vribles .JRf TR” JEE_SEC, JKE_SRCfi0OT are set int ernally depending <?n the JKE wet tin爲Define d classpath ysm abls;ECUPSE_WME - dAProgram Filtseclivse 口E-m treserire

11、d) - D: Progr am F iles.Java jrel - SsJBE_SEC (reserved) - (empty)JRE_SECH3OT Grswerved) - (empty)JVOT 一 0 : Program 卩11馆!£1££14羽£6!”1口£1當.juru !JVHIT_HDHE - d: VFrosran Fileseclipseplnsorg- .JVKTT_SBC i "iFrQgr则 FilesVclipseXplrUsinsVsre. < HYECLIPSE_Ur_HOME - DAPrc

12、gi-am Fil菲VlyEulip弃 5.MYfCLIPSE.STR1H5_HOME - DAPrP6rn Fil?sMdip5 L MYECLIPSE J! F IRE .DAT _HOME - D:Prcgram 氏 1/VlyEE111Nftw.-.fiemoveCancel图1.6 创建变量后的 Classpath Variables窗口2创建Junit测试2.1 创建java项目新建一个 java项目,点击 File -> New -> Project,选择 Java Project 项目,点击Next,为项目起一个名称,例如 Student,如图2.1所示。图2.1

13、创建Student项目再点击Finish,这样就完成新项目的建立了。创建Student类,右键单击Student这个项目名称,在快捷菜单中选择New->Class,在窗口中 Package填入 com.studen,Name填入 Student,如图 2.2所示。abstrac tfinalJava Class匚reate a new Java clas.Sup er cl :Browse, u .Fini shWhi ch miethcid stubs would you like to create?两 public static void mainlStrin£Const

14、ructors from super class帀 Inherited abstract methodEDe you ward to add comments configured in the progfthe current project? Gen&r ate c omm ent e图22 创建Student类再点击Finish,这样就创建了 Student类2.2 建立Junit测试在创建Junit测试之前一定要先导入 junit.jar包。在project Student上单击鼠标右键,选择Properties,如图2.3所示。Studen11由com£ J+ &a

15、mp;. JEEOpen in New WindowOp&n Type Hi erarchyF4:三i CopyCtrl+CFiit*Ctrl+VX HaleteDeleteBuild FathSourceAJLt+Shift+SfactorAlt+Shi £t+T»-Import,.F 勺 Export.EereshF5Close FrcjectGo IntoRun AsAsTt.ainCompare WithRe st or ft fr oni Local History.FEE ToolsFropertiAit+Enter图2.3 Properties命令菜单

16、然后打开Properties for Student的属性对话框,点击Java Build Path项和Libraries选项,如图2.4所示。图 2.4 Properties for Student 对话框再点击 Add Variable 命令按钮,进入 New Variable ClasspathEntry 窗口,选 择JUNIT变量,如图2.5所示图2.5 添加JUNIT变量点击0K命令按钮,完成JUNIT变量的添加。当我们展开一个JUNIT变量结点的时候,能够指定在何处寻找相关联的源代码和Javadoc如图2.6所示图2.6展开后的JUNIT节点为了 Junit能使用其源代码,我们需要

17、 添加Ju nit的源代码,也就是JUNIT_SRC变量,选择结点中的Source attachement,再单击 edit 打开 SourceAttachment Configuration 对话框,如图 2.7所示。图 2.7 SourceAttachment Configuration 对话框然后点击Variable按钮,如图2.8所示。再选择JUNIT_SRC变量,并点击OK关闭此对话框,如图2.9所示Variable Select ion.D电 fined cl as雪pulh var i abl&E:lECLIPSE.mE - i:Frogram Fil«Tecl

18、ips«玄 JEE_UE Reserved) - t: Program Fil&EJavajrel. 6. 0libr G,JBE-SRC (resfirvel) - (epty)-JKE_SECROOT (r«serve- (empty)JUN1T D: VProram FilcsVadipLftVplugiiisVMrg. junit_3.8. 1 JUNIT_HOME -心:Fwgr撫 Filesclipsepluih£org. juni t_3±llff&w.-.Edit.R&mcvese.啰JUNIT-SRC - D:

19、Program FilesVe-clipseVpluginsVorg. eclipc MTECLJFSE_LIB_HONIE - l:Frogram FileEM/Eclip3e G Oyecl v> -图 2.8 Variable Selection 对话框图2.9 添加JUNIT_SRC变量这里我们已使JUnit的源代码能在Eclipse上使用。点击OK关闭Student的属性对话框。现在我们可以看见在工程栏目中 Student项目里已经有JUNIT这样一个结点,如图2.10所示Student-: com. studeikt+ 丄I Student, javi+ Wi. JKH Sy

20、stem Library jrel. G. 0 + J1TWIT - D: XProgran FilesVclipE图2.10 Student工程项目2.3创建StudentTest单元测试为Student类创建Junit测试。在左侧工程导航栏,右键单击 Student.java在快捷菜单中选择 New-> Junit Test Case,如图 2.11 所示。进入 New Junit TestCase窗口,如图2.12所示uJ Student: 击 com. student :3 + 砂 JRE Systin 由査 JUNIT - D:'Student, j w' E2

21、EIiQpm Open WithOpen Typfl Hi er ar chyF3F4Iffi CopyCtrl+dkCtrl+VXD 色.m典门a F*tk1SgTETCeAlt+Shift+SRefactorAlt4Shi£t+TImpor t. ”Expor t.-Refer enc«s卜Declar ati onsEnnmAimota ti皿Oth*r.Ht JVrti t Test Case JUtij t Test SuittUndtlid T«cl Filei Foldtrs皿li& Cl tnG Inttrface衬 SauiT'C

22、e FolderCtrl+N图2.11 选中的Jun it TestCase菜单命令* Mew JUnit Test CaseJUriit Test CaseSelect the 皿创m色 o£ the 麻电横 JUnit test case, Yoii have th电 optiojis to specify the cl ass under t'&s t and on the nextto select methods to be tested.Browse-» Sciirce folder"Package:Studentcom, studen

23、£Browse.,IptudeiLtTgup er cl ass:|juni t. frajmevrcirk TestIDaseWhich rhethod stubs would you like to create?应 public 生tatic vqi d minL(String args)'Add TestEuimer statement for:厂 setp 0tearBown 0enstructor ()t«xt ui * 1Browse.,Class under test: jcom. student. StudentBrowse.HejctFini w

24、hCancel图 2.12 New Junit Test Case窗口在New JunitTestCase窗口,选择我们需要的参数,然后点击Next,在弹出窗口中选择自己需要测试的方法,再点击Finish,如图2.13所示New JHnit Test CaseTest MethodsSelect methods for which test method stubs should be cr电ated.Avai 1 able niethods :StudentAgtlect All3匸百 mainStringl) -口柠 Object 电 C Object 0 ' getClass 0

25、Deselect All 口 口 口一合 hashCod« ()equals (Object) clorifi ()toStr ing () notify () noti fyAll 0 wait QuOng)i method selected.Create Einal method stubsCreate lasks fcr generated test methods< BackFirti stCane al图2.13选择测试的方法然后点击Finish命令按钮,这样我们 StudentTest.java文件就创建完成了。我们现在可以为这个testMain方法添加相应的代码,

26、再运行这个Junit测试。右键单击Stude ntTest.java在快捷菜单中选择 Run as-> Ju nit Test,如图2.14所示。这样我们就可以在左侧的Junit窗口查看测试结果,如图2.15所示。E:心日 田电口旳.slud*nt +1 public class St-udentTest extends: TeatCase main (String! args) 宙 tud.citT*IJ. 1Weg卜OpenP3Open Vi tbOpen. Type Hi erarchyF4CopyCtrl+Ci LasteCtrl+VXmm*Build PilhSowrc*JUt+Shi fttS*RefactorAlttSlu £ttT込1 Imparl.£ Expr t.Refer &Aet£>Beclaiatioits汐 Befmihreun A sI叩&TitmCnptr* Ti th卜Re

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论