How REST Assured Integrates with TestNG
REST Assured is responsible for sending API requests and validating responses, while TestNG manages test execution, sequencing, grouping, reporting, and the overall test lifecycle.
Together, they form a robust API automation framework.
Interview Answer
"REST Assured handles API requests and response validation, while TestNG manages test execution, annotations, grouping, prioritization, and reporting. In our framework, each API test case is written inside a TestNG
@Testmethod using REST Assured. TestNG controls the execution flow through annotations and thetestng.xmlfile."Advertisement
Integration Architecture
REST Assured
│
▼
API Request
│
▼
Response Validation
│
▼
TestNG
│
├── Test Execution
├── Annotations
├── Grouping
├── Prioritization
├── Parallel Execution
└── Reporting
Responsibilities
| Component | Responsibility |
|---|---|
| REST Assured | Send API requests and validate responses |
| TestNG | Execute tests, manage lifecycle, grouping, prioritization, and reporting |
testng.xml |
Controls execution order, suites, groups, and parallel execution |
Typical Project Flow
@BeforeSuite
│
▼
@BeforeClass
│
▼
@Test
│
▼
REST Assured API Call
│
▼
Assertions
│
▼
@AfterClass
│
▼
@AfterSuite
Why Use TestNG?
TestNG provides several features that make it ideal for API automation:
- Organized test execution
- Test lifecycle management
- Parallel execution
- Test grouping
- Test prioritization
- Data-driven testing
- Retry mechanisms
- Detailed reports
When an assertion passes, TestNG marks the test as PASS.
When an assertion fails, TestNG marks the test as FAIL.
Real-Time Example
In our framework, we created TestNG classes where each
@Testmethod contains REST Assured API calls. The execution order, smoke suites, regression suites, and parallel execution were controlled using thetestng.xmlfile.
TestNG Annotations Used
TestNG annotations define when a method should execute during the test lifecycle.
Common TestNG Annotations
| Annotation | Runs | Purpose |
|---|---|---|
@BeforeSuite |
Once before the entire suite | Global setup |
@BeforeClass |
Once before all tests in a class | Class-level setup |
@Test |
For every test method | Execute API test |
@AfterClass |
Once after all tests in a class | Class-level cleanup |
@AfterSuite |
Once after the entire suite | Global cleanup |
Test Lifecycle
@BeforeSuite
│
▼
@BeforeClass
│
▼
@Test
│
▼
@Test
│
▼
@Test
│
▼
@AfterClass
│
▼
@AfterSuite
Example
@BeforeSuite
public void beforeSuite(){
}
@BeforeClass
public void setup(){
}
@Test
public void verifyUser(){
}
@AfterClass
public void cleanup(){
}
@AfterSuite
public void afterSuite(){
}
Why Are Annotations Important?
They help:
- Initialize prerequisites
- Execute test cases
- Avoid duplicate setup
- Perform cleanup
- Improve framework organization
@BeforeClass and @AfterClass
These are the most commonly used annotations in REST Assured frameworks.
@BeforeClass
Runs once before all test methods in a class.
Common uses:
- Set
RestAssured.baseURI - Generate authentication token
- Initialize request specification
- Read configuration
- Create reusable objects
Example
@BeforeClass
public void setup(){
RestAssured.baseURI = "https://api.example.com";
// Generate authentication token
}
@AfterClass
Runs once after all test methods in the class.
Common uses:
- Clear authentication token
- Reset variables
- Close sessions
- Log execution summary
- Release resources
Example
@AfterClass
public void cleanup(){
// Clear token
// Cleanup resources
}
Execution Flow
@BeforeClass
│
▼
Generate Token
Set Base URI
│
▼
@Test
│
▼
@Test
│
▼
@Test
│
▼
@AfterClass
│
▼
Clear Token
Cleanup
Why Use @BeforeClass?
Instead of repeating setup before every test:
❌ Generate token before every API
✔ Generate token once
Benefits:
- Faster execution
- Less duplicate code
- Better maintenance
- Cleaner framework
Real-Time Example
In my project,
@BeforeClassinitialized theRestAssured.baseURIand generated the JWT token. All@Testmethods reused this setup, and@AfterClasscleared the token and logged the completion of the test execution.
@BeforeClass vs @AfterClass
| Annotation | Execution | Purpose | Typical Use |
|---|---|---|---|
@BeforeClass |
Once before all tests | Setup | Base URI, authentication token, request specification |
@AfterClass |
Once after all tests | Cleanup | Clear token, close sessions, release resources |
Prioritizing API Test Cases
Prioritization determines the order in which TestNG executes test cases.
It is useful when:
- APIs have dependencies
- Business-critical APIs should run first
- Smoke tests should execute before regression tests
TestNG Priority
@Test(priority = 1)
public void login(){
}
@Test(priority = 2)
public void createUser(){
}
@Test(priority = 3)
public void updateUser(){
}
@Test(priority = 4)
public void deleteUser(){
}
Execution Order
Priority 1
│
▼
Priority 2
│
▼
Priority 3
│
▼
Priority 4
Why Prioritize?
Benefits include:
- Business-critical APIs run first.
- Dependent APIs execute in sequence.
- Faster failure detection.
- Better regression execution.
Real-Time Example
In our regression suite, the Login API executes first, followed by Create User, Update User, and Delete User APIs. This ensures dependent APIs execute in the correct sequence.
Grouping Test Cases
TestNG allows test cases to be grouped logically.
Typical groups include:
- Smoke
- Sanity
- Regression
- Integration
Group Example
@Test(groups = "smoke")
public void login(){
}
@Test(groups = "regression")
public void createUser(){
}
Group Execution
testng.xml
<suite name="API Suite">
<test name="Smoke Tests">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
<classes>
<class name="tests.UserTest"/>
</classes>
</test>
</suite>
Grouping Flow
Smoke
│
▼
Sanity
│
▼
Regression
│
▼
Integration
Advantages of Grouping
- Execute only required test cases.
- Separate smoke and regression suites.
- Faster CI/CD execution.
- Easier maintenance.
- No duplicate test classes.
Real-Time Example
In our CI/CD pipeline, the Smoke suite executes after every build, while the complete Regression suite runs overnight. This is achieved using TestNG groups configured through the
testng.xmlfile.
Best Practices
- Use
@BeforeClassfor reusable setup such as Base URI and authentication. - Use
@AfterClassfor cleanup. - Keep one API scenario per
@Testmethod. - Use priorities only when tests have execution dependencies.
- Prefer grouping (
smoke,sanity,regression) over excessive use of priorities. - Use
testng.xmlto control suite execution and parallel execution. - Design tests to be as independent as possible.
Frequently Asked Questions (FAQs)
1. How does REST Assured integrate with TestNG?
REST Assured is used inside TestNG @Test methods to send API requests and validate responses. TestNG manages the test lifecycle, execution order, annotations, grouping, prioritization, reporting, and suite execution using testng.xml.
2. Which TestNG annotations are commonly used with REST Assured?
The most commonly used annotations are:
@BeforeSuite@BeforeClass@Test@AfterClass@AfterSuite
These annotations handle setup, execution, and cleanup activities within the API automation framework.
3. What are @BeforeClass and @AfterClass?
@BeforeClassexecutes once before all test methods in a class. It is commonly used to initialize the Base URI, generate authentication tokens, and perform setup activities.@AfterClassexecutes once after all test methods in the class. It is used to clear tokens, close sessions, release resources, and perform cleanup.
4. Why use @BeforeClass instead of performing setup in every test?
Using @BeforeClass avoids repetitive initialization, reduces execution time, improves code maintainability, and ensures that common setup activities such as generating authentication tokens or configuring the Base URI are executed only once.
5. How do you prioritize API test cases?
TestNG provides the priority attribute within the @Test annotation.
Example:
@Test(priority = 1)
This allows business-critical or dependent APIs to execute before lower-priority test cases.
6. How do you group test cases?
Test cases can be grouped using the groups attribute of the @Test annotation.
Example:
@Test(groups = "smoke")
The required groups are then executed through the testng.xml file, allowing separate execution of Smoke, Sanity, and Regression suites without maintaining separate test classes.