OkHttp3 logo

OkHttp3

Open Source

HTTP client for Java/Kotlin with built-in MockWebServer for testing HTTP-dependent code.

Visit websiteGitHub

Pricing

Free / Open source

Type

Automation

Languages

Java, Kotlin

// VERDICT

Reach for OkHttp3 when you need a robust HTTP client in Java/Kotlin code, including in tests, with its built-in MockWebServer for stubbing. Skip it when you want a higher-level API-test DSL (REST Assured) or a GUI client.

Best for

An efficient HTTP client library for JVM/Android apps - the underlying client many Java/Kotlin tests and tools use to make requests.

Avoid when

You want a dedicated test-assertion framework or a GUI client rather than an HTTP client library.

CI/CD fit

JUnit · Maven · Gradle · GitHub Actions

Languages

Java · Kotlin

Team fit

Java/Kotlin teams · Android teams · Backend developers

Setup

Easy

Maintenance

Low

Learning

Intermediate

Licence

Free / Open source

// BEST FOR

  • Making HTTP calls from Java/Kotlin application and test code
  • Android networking where OkHttp is a de-facto standard
  • Stubbing servers in tests via the bundled MockWebServer
  • Fine-grained control over connections, interceptors and timeouts
  • Underpinning higher-level tools and clients on the JVM
  • Efficient connection pooling and HTTP/2 support

// AVOID WHEN

  • You want a higher-level API-testing DSL (REST Assured fits)
  • You need a GUI client for exploring APIs
  • Non-developers must author or run the checks
  • You're not on the JVM/Android
  • You want a full assertion/reporting framework out of the box
  • Contract or end-to-end testing is the actual goal

// QUICK START

# Gradle: implementation("com.squareup.okhttp3:okhttp")
#         testImplementation("com.squareup.okhttp3:mockwebserver")
./gradlew test

// ALTERNATIVES TO CONSIDER

ToolChoose it when
REST AssuredYou want a higher-level given/when/then API-testing DSL in Java.
WebTestClientYou're testing Spring WebFlux/MVC endpoints in-framework.
SuperTestYou're in JS/Node rather than the JVM.

// FEATURES

  • MockWebServer for scripting HTTP responses in tests
  • Interceptor chain for inspecting and modifying requests
  • Connection pooling, HTTP/2, and SOCKS support
  • Built-in caching with conditional GET handling
  • Lightweight test mode without spinning up real servers

// PROS

  • MockWebServer makes HTTP-dependent code easy to test
  • Used as the default HTTP client in many Android and JVM apps
  • Active Square maintenance and stable API
  • Pairs well with Retrofit and Moshi for full client testing

// CONS

  • JVM-only — no use outside Java/Kotlin
  • MockWebServer scripting verbose for complex flows
  • Adds dependency weight if your code doesn't already use OkHttp

// EXAMPLE QA WORKFLOW

  1. Add OkHttp (and MockWebServer for tests) to the build

  2. Build requests with the client in code

  3. Configure interceptors and timeouts as needed

  4. Use MockWebServer to stub responses in tests

  5. Assert on responses within JUnit

  6. Run as part of the normal JVM test step in CI

// RELATED QA.CODES RESOURCES