This project accompanies a two-part blog series by T.J. Maher from "Adventures in Automation" exploring API testing with REST Assured as an alternative to unreliable UI testing:
The first blog post (February 2017) tackles a common testing challenge: verifying that the MBTA (Massachusetts Bay Transportation Authority) bus line #230 is still operational. Rather than relying on the slow and unreliable MBTA website interface, which often leads to "flaky tests" due to synchronization issues and timeouts, T.J. demonstrates a better approach using RESTful API endpoints.
The MBTA provides a Developer Portal with real-time data access, including:
- Bus schedules and routes
- Real-time vehicle positions
- Stop information and predictions
- Service alerts
The second blog post (March 2017) introduces REST Assured as a Java library for simplified API testing. REST Assured provides several advantages over manual HTTP connection handling:
- Eliminates boilerplate code for HTTP connections
- Supports Given/When/Then test notation for human-readable tests
- Integrates seamlessly with Java testing frameworks like TestNG and JUnit
- Enables fast, reliable API tests that complete in seconds rather than minutes
REST Assured is a Java library developed by Johan Haleby for testing RESTful APIs. It simplifies complex API interactions with its fluent interface and integrates well with testing frameworks like TestNG and JUnit. The library supports all HTTP methods, JSON/XML parsing, and various authentication mechanisms.
This project demonstrates automated API testing for the MBTA (Massachusetts Bay Transportation Authority) public transit system. The tests validate that bus route #230 is properly listed in the MBTA's real-time API system, serving as a practical example of API testing with REST Assured.
- ✅ API response format validation (JSON)
- ✅ Authentication with API keys
- ✅ HTTP response codes (200 OK, 401 Unauthorized)
- ✅ Response time performance (< 2 seconds)
- ✅ Data verification (Bus #230 exists in system)
RestAssuredJavaDemo/
├── build.gradle # Gradle build configuration
├── settings.gradle # Gradle project settings
├── gradlew # Gradle wrapper (Unix)
├── gradlew.bat # Gradle wrapper (Windows)
├── gradle/
│ └── wrapper/
│ └── gradle-wrapper.properties # Gradle wrapper config
├── src/
│ └── test/
│ └── java/
│ └── MbtaRoutesTest.java # Main REST Assured test class
└── build/
└── classes/
└── test/ # Compiled test classes
The main test class containing five comprehensive API tests:
checkResponseContentTypeIsJson()- Validates JSON content typecheckResponseCodeIsOk200()- Verifies successful HTTP responsecheckUserCannotAccessApiWithoutKey()- Tests API securitycheckResponseTimeIsLessThan2seconds()- Performance validationcheckThatRoute230isListed()- Business logic verification
Gradle build configuration with dependencies:
- REST Assured 3.0.1 for API testing
- TestNG 6.3.1 for test framework
- Hamcrest 2.0.0.0 for readable assertions
- JSON Path support for response parsing
| Library | Version | Purpose |
|---|---|---|
| REST Assured | 3.0.1 | HTTP/REST API testing framework |
| TestNG | 6.3.1 | Testing framework with annotations |
| Hamcrest | 2.0.0.0 | Matcher library for assertions |
| JSON Path | 3.0.1 | JSON parsing and querying |
| JUnit | 4.11 | Alternative testing framework |
Base URL: http://realtime.mbta.com/developer/api/v2/
Public API Key: wX9NwuHnZU2ToO7GmGR9uw (for testing only)
Documentation: MBTA Developer Portal
⚠️ Note: The public API key is for development/testing only and may change. For production applications, register for your own API key at the MBTA Developer Portal.
/routes- Retrieves all transportation routes (buses, subway, commuter rail)
The #230 bus line connects:
- Start: Montello Commuter Rail Station (Brockton, MA)
- End: Quincy Center Station
- Intermediate: Braintree MBTA Station
- Service Area: Four cities across Southeastern Massachusetts
- Java 8+
- Gradle (or use included wrapper)
# Using Gradle wrapper (recommended)
./gradlew testNG # Unix/Mac
gradlew.bat testNG # Windows
# Using local Gradle installation
gradle testNGTests generate HTML reports in build/reports/testng/
Expected output:
✅ All 5 tests pass
⏱️ Total execution time: < 3 seconds
🚌 Bus #230 confirmed as listed in MBTA system
@Test
public void checkResponseContentTypeIsJson()Verifies API returns application/json content type.
@Test
public void checkResponseCodeIsOk200() // Valid API key → 200 OK
@Test
public void checkUserCannotAccessApiWithoutKey() // Invalid key → 401 Unauthorized@Test
public void checkResponseTimeIsLessThan2seconds()Ensures API responds within acceptable time limits.
@Test
public void checkThatRoute230isListed()Validates that Bus #230 exists in the "Bus" mode routes.
{
"mode": [
{
"route_type": "3",
"mode_name": "Bus",
"route": [
{
"route_id": "230",
"route_name": "230"
}
]
}
]
}- API Testing > UI Testing: Direct API testing is faster, more reliable, and less flaky than UI automation
- REST Assured Benefits: Simplified HTTP interactions with readable Given/When/Then syntax
- Comprehensive Validation: Test authentication, performance, data format, and business logic
- Public APIs: Many organizations provide APIs for third-party integration and testing
Created by: T.J. Maher (@tjmaher1)
Blog Series: Adventures in Automation
Based on: Bas Dijkstra's REST Assured Workshop
This project is available for educational and demonstration purposes. Feel free to modify and adapt the code for your own API testing projects.