
Eclipse -> File -> New -> Java Project
建立1個Java專案或Maven專案後,在package上點右鍵,會出現選單,New -> JUnit Test Case。
- 選New JUnit 4 Test,當然如果你習慣用舊版的,也可以選New JUnit 3 Test。
- Name:打上你要取的class名稱,如:JunitTest,慣例上是以Test結尾。
- 要自動產生哪些method,全選或都不選都可,產生後還是可以刪除。
- 按下Finish,這樣就完成了1支Test Case Class。

- setUpBeforeClass(),class初始化之後調用,用來作測試的準備工作。
- tearDownAfterClass(),class結束之前調用,用來作測試的清理工作。
- setUp() ,在測試method前調用,用來作測試的準備工作
- tearDown() 在測試method後調用,用來作測試的清理工作。
package org.openyu.java.junit; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; /** * The Class JunitTest. */ public class JunitTest { /** * Sets the up before class. * * @throws Exception * the exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { } /** * Tear down after class. * * @throws Exception * the exception */ @AfterClass public static void tearDownAfterClass() throws Exception { } /** * Sets the up. * * @throws Exception * the exception */ @Before public void setUp() throws Exception { } /** * Tear down. * * @throws Exception * the exception */ @After public void tearDown() throws Exception { } /** * Test. */ @Test public void test() { System.out.println("---------------------------"); System.out.println("test java.openyu.org"); System.out.println("---------------------------"); } }
Mix在test()上點右鍵,會出現選單,Run As-> JUnit Test,就可執行了。
或是用熱鍵按下Alt+Shift+X,再按下一T,一樣可執行。
console查看其結果。
--------------------------- test java.openyu.org ---------------------------
沒有留言:
張貼留言