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.
@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.
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;
}