2013-10-23 122 views
0

我有但我不知道它是怎麼開始執行,因爲它不包含主或任何亞軍類功能這工作絕對沒問題的樣本JUnit測試用例「主」的功能。任何人都可以分享如何執行JUnit測試用例沒有主要功能哪裏調用的JUnit測試案例

的代碼是:

import static org.junit.Assert.assertEquals; 

import org.junit.After; 
import org.junit.AfterClass; 
import org.junit.Before; 
import org.junit.BeforeClass; 
import org.junit.Ignore; 
import org.junit.Test; 

public class testJUnit { 

@BeforeClass 
public static void setUpBeforeClass() throws Exception { 
    System.out.println("Before Class"); 
} 

@AfterClass 
public static void tearDownAfterClass() throws Exception { 
    System.out.println("Tear Down After Class"); 
} 

@Before 
public void setUp() throws Exception { 
    System.out.println("Setup"); 
} 

@After 
public void tearDown() throws Exception { 
    System.out.println("Tear Down"); 
} 

@Test 
public void test() { 
    int a = 1; 
    assertEquals("Testing...", 1, a); 
} 

@Ignore 
@Test 
public void test1() { 
    int a = 156; 
    assertEquals("Testing...", 156, a); 
} 

} 

回答

2

當運行在Eclipse JUnit測試時,Eclipse運行它有它自己的主類其他JVM(RemoteTestRunner如果你有興趣)。這與JUnitCore有效地執行相同的工作,但是對於特定於Eclipse的更改 - 它需要將結果傳遞迴Eclipse。

欲瞭解更多信息,請參閱我的回答How does Eclipse actually run Junit tests?

+0

謝謝@Matthew :) – Amrit

2

當你運行一個JUnit測試,你實際上執行org.junit.runner.JUnitCore並提供您完整的類名作爲參數。

您可能正在使用IDE,例如Eclipse或Netbeans,它可以爲您處理所有邏輯。有關此主題,請參閱@Matthew Farwell answer

來源:jUnit FAQ (2006)

+0

其實,從Eclipse中運行時,它不會調用JUnitCore。你引用的FAQ是從2006年起。 –

+0

編輯我的回答 –

0

的類不需要一個主要方法。 jUnit包含一個用於執行的主要方法,並且在某個時候,jUnit會創建一個測試類的實例。

0

它由框架本身處理。執行發生在@Before,@測試& @After的順序..