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 企业应用

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