Eclipse的Junit配置_第1頁
Eclipse的Junit配置_第2頁
Eclipse的Junit配置_第3頁
Eclipse的Junit配置_第4頁
Eclipse的Junit配置_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、叵區(qū)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(更多細(xì)節(jié)請參考 ),我們還要在Eclipse中進(jìn)行一些設(shè)置。其中主要是就是類路徑變量的設(shè)置1.1 倉U建變量 JUNIT 和 JUNIT_SRC在project的classpath中添加junit.jar。因此,建立一個新的 classpath變量JUNIT。選擇 Window -> Preferences 菜單欄打開 Preferences對話框,如圖 1.1所示。展開 Java 結(jié)點,選擇 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 創(chuàng)建JUNIT變量為了 debug的需要,也要添加JUnit包的源代碼。您將在 /eclipse/plugi ns/org.eclipse.jdt.source_3.0.2/src 目錄

8、中發(fā)現(xiàn)許多包的源代碼的 JAR 文檔。為JUnit源代碼創(chuàng)建一個新的變量,按照上面創(chuàng)建 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 創(chuàng)建JUNIT_SRC變量單擊0K命令按鈕,這樣兩個環(huán)境變量就創(chuàng)建完成,我們可以看見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爲(wèi)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當(dāng).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 創(chuàng)建變量后的 Classpath Variables窗口2創(chuàng)建Junit測試2.1 創(chuàng)建java項目新建一個 java項目,點擊 File -> New -> Project,選擇 Java Project 項目,點擊Next,為項目起一個名稱,例如 Student,如圖2.1所示。圖2.1

13、創(chuàng)建Student項目再點擊Finish,這樣就完成新項目的建立了。創(chuàng)建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 創(chuàng)建Student類再點擊Finish,這樣就創(chuàng)建了 Student類2.2 建立Junit測試在創(chuàng)建Junit測試之前一定要先導(dǎo)入 junit.jar包。在project Student上單擊鼠標(biāo)右鍵,選擇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 命令按鈕,進(jìn)入 New Variable ClasspathEntry 窗口,選 擇JUNIT變量,如圖2.5所示圖2.5 添加JUNIT變量點擊0K命令按鈕,完成JUNIT變量的添加。當(dāng)我們展開一個JUNIT變量結(jié)點的時候,能夠指定在何處尋找相關(guān)聯(lián)的源代碼和Javadoc如圖2.6所示圖2.6展開后的JUNIT節(jié)點為了 Junit能使用其源代碼,我們需要

17、 添加Ju nit的源代碼,也就是JUNIT_SRC變量,選擇結(jié)點中的Source attachement,再單擊 edit 打開 SourceAttachment Configuration 對話框,如圖 2.7所示。圖 2.7 SourceAttachment Configuration 對話框然后點擊Variable按鈕,如圖2.8所示。再選擇JUNIT_SRC變量,并點擊OK關(guān)閉此對話框,如圖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關(guān)閉Student的屬性對話框?,F(xiàn)在我們可以看見在工程欄目中 Student項目里已經(jīng)有JUNIT這樣一個結(jié)點,如圖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創(chuàng)建StudentTest單元測試為Student類創(chuàng)建Junit測試。在左側(cè)工程導(dǎo)航欄,右鍵單擊 Student.java在快捷菜單中選擇 New-> Junit Test Case,如圖 2.11 所示。進(jìn)入 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 皿創(chuàng)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?應(yīng) 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窗口,選擇我們需要的參數(shù),然后點擊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文件就創(chuàng)建完成了。我們現(xiàn)在可以為這個testMain方法添加相應(yīng)的代碼,

26、再運行這個Junit測試。右鍵單擊Stude ntTest.java在快捷菜單中選擇 Run as-> Ju nit Test,如圖2.14所示。這樣我們就可以在左側(cè)的Junit窗口查看測試結(jié)果,如圖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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論