Common java testing frameworks

Source: Internet
Author: User
Tags testng

Common java testing frameworks
1. Common Unit testing frameworks junit4 and TestNG

You can use the annotation @ Before @ After @ BeforeClass @ AfterClass to initialize and end methods and classes respectively.
TestNG example:

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

The execution sequence is

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

For a large number of test classes, you can specify the classes to be tested to use the package test.

@ RunWith (Suite. class) // specify the suit test runner @ Suite. SuiteClasses ({Junit4TimeoutTest. class, Junit4ExceptionTest. class}) public class Junit4SuiteTest {}

In this way, Junit4TimeoutTest and Junit4ExceptionTest are packaged and tested separately.

2. Simulate the mockito object and perform interactive verification.

Mock supports mock interfaces and implementation classes.
Verify verification process is called

Sample mockedSample = mock (Sample. class); // It can be an interface or an implementation class // verify the interaction when (mockedSample. getName ()). thenReturn ("sample"); verify (mockSampleService, atMost (1 )). getName (); // one verification method call
3. Unitils

You can integrate spring, db (MYSQL, HIBERIATE), and various third-party testing frameworks (junit 4, testNG) through modular configuration)

4. Test the web layer

Spring mock provides simulation classes for container-dependent interfaces, allowing you to perform unit tests without starting the container.
Org. springframework. mock. jndi provides a simulation class for the jndi spi to get rid of the dependency on the java ee container.
Org. springframework. mock. web provides a simulation class (HttpServletRequest, ServletContext) for servlet api interfaces, which is separated from servlet container testing.

5. Simulate client requests

Spring RestTemplate
RestTemplate is a class used to access web services on the client.

@ Before public void init () {driver = new HtmlUnitDriver (); // IE} @ Test public void loginCheck () {// After the page is fully loaded, the control is returned to the test Script driver. get ("http: // localhost/index.html"); // element = driver. findElement (. xpath ("// input [@ id = 'xxx']"); WebElement userName = driver. findElement (. name ("userName"); WebElement password = driver. findElement (. name ("password"); // any page element can call sendKeys, userName. sendKeys ("tom"); password. sendKeys ("1234"); // submit the form driver. findElement (. id ("loginBtn ")). click (); // driver. findElement (. id ("submit ")). submit (); requires that the element must be in the form; otherwise, NoSuchElementException will be thrown. // verify that the main page is returned. jsp assertThat (driver. getTitle (), failed to ("this's title"); assertThat (driver. getPageSource (), containsString ("tom"); WebElement body = driver. findElement (. xpath ("// body"); assertThat (body. getText (), containsString ("tom, welcome "));}

The above example introduces spring3.x enterprise applications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.