java 常用測試架構

來源:互聯網
上載者:User

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");  }}

其執行順序為

in beforeSuitein beforeTestin beforeClassin beforeMethodin test case 1in afterMethodin beforeMethodin test case 2in afterMethodin afterClassin afterTestin afterSuite

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

@RunWith(Suite.class) //指定suit測試回合器@Suite.SuiteClasses({Junit4TimeoutTest.class,Junit4ExceptionTest.class})public class Junit4SuiteTest {}

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

2. mockito 類比對象,並做互動驗證

mock 可以mock介面和實作類別
verify 驗證過程是否被調用

Sample mockedSample = mock(Sample.class);//可以是介面或者是實作類別//驗證互動when(mockedSample.getName()).thenReturn("sample");verify(mockSampleService, atMost(1)).getName(); // 驗證方法之多調用了1次
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"));    }

上述樣本引子spring3.x 公司專屬應用程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.