Skip to content

tjmaher/RestAssuredJavaDemo

Repository files navigation

REST Assured Java Demo

Blog Series Summary

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:

Part 1: The Problem with 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

Part 2: REST Assured Implementation

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

About REST Assured

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.

Project Overview

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.

Key Features Tested

  • ✅ 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)

Project Structure

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

Core Files

MbtaRoutesTest.java

The main test class containing five comprehensive API tests:

  1. checkResponseContentTypeIsJson() - Validates JSON content type
  2. checkResponseCodeIsOk200() - Verifies successful HTTP response
  3. checkUserCannotAccessApiWithoutKey() - Tests API security
  4. checkResponseTimeIsLessThan2seconds() - Performance validation
  5. checkThatRoute230isListed() - Business logic verification

build.gradle

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

Dependencies

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

MBTA API Details

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.

API Endpoints Used

  • /routes - Retrieves all transportation routes (buses, subway, commuter rail)

Route #230 Details

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

Running the Tests

Prerequisites

  • Java 8+
  • Gradle (or use included wrapper)

Execute Tests

# Using Gradle wrapper (recommended)
./gradlew testNG          # Unix/Mac
gradlew.bat testNG        # Windows

# Using local Gradle installation
gradle testNG

Test Results

Tests 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 Scenarios Covered

1. Content Type Validation

@Test
public void checkResponseContentTypeIsJson()

Verifies API returns application/json content type.

2. Authentication & Authorization

@Test  
public void checkResponseCodeIsOk200()      // Valid API key → 200 OK
@Test
public void checkUserCannotAccessApiWithoutKey()  // Invalid key → 401 Unauthorized

3. Performance Testing

@Test
public void checkResponseTimeIsLessThan2seconds()

Ensures API responds within acceptable time limits.

4. Data Integrity

@Test
public void checkThatRoute230isListed()

Validates that Bus #230 exists in the "Bus" mode routes.

Sample API Response Structure

{
  "mode": [
    {
      "route_type": "3",
      "mode_name": "Bus", 
      "route": [
        {
          "route_id": "230",
          "route_name": "230"
        }
      ]
    }
  ]
}

Key Learning Points

  1. API Testing > UI Testing: Direct API testing is faster, more reliable, and less flaky than UI automation
  2. REST Assured Benefits: Simplified HTTP interactions with readable Given/When/Then syntax
  3. Comprehensive Validation: Test authentication, performance, data format, and business logic
  4. Public APIs: Many organizations provide APIs for third-party integration and testing

Author & References

Created by: T.J. Maher (@tjmaher1)
Blog Series: Adventures in Automation
Based on: Bas Dijkstra's REST Assured Workshop

Related Resources

License

This project is available for educational and demonstration purposes. Feel free to modify and adapt the code for your own API testing projects.

About

Introductory project in Java library REST Assured

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages