java 常用測試框架

1. 常用單元化測試框架 junit4 , TestNG

可以通過註解 @Before @After @BeforeClass @AfterClass 分別作方法與類級的初始化與結束動作。 
testNG示例:

public class TestngAnnotation {
  // test case 1
  @Test
  public void testCase1() {
      System.out.println("in test case 1");
  }
  // test case 2
  @Test
  public void testCase2() {
      System.out.println("in test case 2");
  }
  @BeforeMethod
  public void beforeMethod() {
      System.out.println("in beforeMethod");
  }
  @AfterMethod
  public void afterMethod() {
      System.out.println("in afterMethod");
  }
  @BeforeClass
  public void beforeClass() {
      System.out.println("in beforeClass");
  }
  @AfterClass
  public void afterClass() {
      System.out.println("in afterClass");
  }
  @BeforeTest
  public void beforeTest() {
      System.out.println("in beforeTest");
  }
  @AfterTest
  public void afterTest() {
      System.out.println("in afterTest");
  }
  @BeforeSuite
  public void beforeSuite() {
      System.out.println("in beforeSuite");
  }
  @AfterSuite
  public void afterSuite() {
      System.out.println("in afterSuite");
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

其執行順序爲

in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

對於測試類較多時,可以指定需測試的類 使用打包測試

@RunWith(Suite.class) //指定suit測試運行器
@Suite.SuiteClasses({Junit4TimeoutTest.class,Junit4ExceptionTest.class})
public class Junit4SuiteTest {
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

如此對 Junit4TimeoutTest 與 Junit4ExceptionTest 進行打包測試,無需一個個分別測試。

2. mockito 模擬對象,並做交互驗證

mock 可以mock接口和實現類 
verify 驗證過程是否被調用

Sample mockedSample = mock(Sample.class);//可以是接口或者是實現類
//驗證交互
when(mockedSample.getName()).thenReturn("sample");
verify(mockSampleService, atMost(1)).getName(); // 驗證方法之多調用了1次
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

3. Unitils

可以通過模塊化的配置,集成 spring, db (MySQL , HIBERIATE) 以及 各種第三方測試框架(junit 4, testNG)

4. 測試web層

spring mock中 爲一些依賴於容器的接口提供了模擬類,可以使用戶在不啓動容器的情況下 執行單元測試。 
org.springframework.mock.jndi 爲jndi spi提供模擬類,擺脫對Java EE容器的依賴 
org.springframework.mock.web 爲servlet api接口提供模擬類(HttpServletRequest, ServletContext),脫離servlet容器測試

5. 客戶端請求模擬

spring RestTemplate 
RestTemplate 是用來在客戶端訪問web服務的類。

    @Before
    public void init() {
        driver = new HtmlUnitDriver(); //IE
    }
    @Test
    public void loginCheck(){
        //完全裝載頁面後將控制返回給測試腳本
        driver.get("http://localhost/index.html"); 
        //element = driver.findElement(By.xpath( "//input[@id=’xxx’]" ));
        WebElement userName = driver.findElement(By.name("userName"));  
        WebElement password = driver.findElement(By.name("password"));
        //任何頁面元素都可以調用sendKeys,
        userName.sendKeys("tom");     
        password.sendKeys("1234");  
        //提交表單
        driver.findElement(By.id( "loginBtn" )).click();
        //driver.findElement(By.id( "submit" )).submit(); 要求element必須在表單中,否則拋出NoSuchElementException
        //驗證返回的主頁面 main.jsp
        assertThat(driver.getTitle(), equalTo("this's title"));
        assertThat(driver.getPageSource(), containsString("tom"));
        WebElement body  = driver.findElement(By.xpath( "//body" ));
        assertThat(body.getText(), containsString("tom,welcome"));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

上述示例引子spring3.x 企業應用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章