Testing with TestNG



Fail Fast Using TestListenerAdapter
Sometimes, we want to all suites stops immediately if one test fails, TestListenerAdapter can help on this. onTestFailure will be called when any test fails, it will set static boolean varible hasFailedTest to true.
public class MyTestNGListener extends TestListenerAdapter {
    private static boolean hasFailedTest;
    @Override
    public void beforeConfiguration(final ITestResult result) {
        if (hasFailedTest) {
            throw new SkipException("Skipping beforeConfiguration");
        }
        super.beforeConfiguration(result);
    }

    @Override
    public void onStart(final ITestContext testContext) {
        if (hasFailedTest) {
            throw new SkipException("Skipping onStart");
        }
        super.onStart(testContext);
    }

    @Override
    public void onTestStart(final ITestResult result) {
        if (hasFailedTest) {
            throw new SkipException("Skipping onTestStart");
        }
        super.onTestStart(result);
    }

    @Override
    public void onTestFailure(final ITestResult tr) {
        hasFailedTest = true;
    }
}

@Listeners({MyTestNGListener.class})
public abstract class BaseTest {}

@BeforeSuite + @BeforeClass
To test admin api which requires login first, we login in the @BeforeSuite and share the session in all test in the suite.
-- We use rest-assured to do end-2-end rest api testing.

To make the child class easier to use the sessionFilter, we use @BeforeClass to store it as instance variable.
public abstract class BaseTest {
  @BeforeSuite(alwaysRun = true)
  @Parameters({"adminBaseUrl", "clientBaseUrl", "username", "password"})
  public void setup(final String adminBaseUrl, final String username, final String password,
          final ITestContext context) {
      final SessionFilter sessionFilter = TestUtil.configureAdminRestAssured(adminBaseUrl, username, password);
      context.setAttribute(FILTER_NAME, sessionFilter);
  }

  protected RequestSpecification restRequestSpec, adminRequestSpec;
  protected SessionFilter sessionFilter;

  @BeforeClass(alwaysRun = true)
  @Parameters({"adminBaseUrl", "clientBaseUrl"})
  public void beforeClass(final String adminBaseUrl, final String clientBaseUrl, final ITestContext context) {
      restRequestSpec = new RequestSpecBuilder().setBaseUri(clientBaseUrl).setContentType(ContentType.JSON)
              .setRelaxedHTTPSValidation().build();

      adminRequestSpec = new RequestSpecBuilder().setBaseUri(adminBaseUrl).setContentType(ContentType.JSON)
              .setRelaxedHTTPSValidation().build();

      sessionFilter = (SessionFilter) context.getAttribute(FILTER_NAME);
  }
}

public static SessionFilter configureAdminRestAssured(final String baseUrl, final String username,
        final String password) {
    RestAssured.reset();
    RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
    RestAssured.config = config().logConfig(new LogConfig());
    RestAssured.config.getHttpClientConfig().reuseHttpClientInstance();
    RestAssured.config = config().sslConfig(sslConfig().allowAllHostnames());
    RestAssured.useRelaxedHTTPSValidation();
    
    final SessionFilter sessionFilter = new SessionFilter();
    final Map<string object> parameters = new HashMap<>();
    parameters.put("j_username", username);
    parameters.put("j_password", password);

    // if login successful, it will redirect it to index.html, check whether its status is 302,
    // the new location ends with index.html
    final ExtractableResponse<response> response =
            given().contentType(ContentType.URLENC).formParameters(parameters).filter(sessionFilter)
            .post(baseUrl + "/j_spring_security_check").then().statusCode(302).extract();
    assertThat(response.header("Location"), endsWith("index.html"));
    RestAssured.basePath = "/v1";
    return sessionFilter;
}


Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)