OkHttp3
HTTP client for Java/Kotlin with built-in MockWebServer for testing HTTP-dependent code.
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
Maintenance
Learning
Licence
// 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
| Tool | Choose it when |
|---|---|
| REST Assured | You want a higher-level given/when/then API-testing DSL in Java. |
| WebTestClient | You're testing Spring WebFlux/MVC endpoints in-framework. |
| SuperTest | You'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
Add OkHttp (and MockWebServer for tests) to the build
Build requests with the client in code
Configure interceptors and timeouts as needed
Use MockWebServer to stub responses in tests
Assert on responses within JUnit
Run as part of the normal JVM test step in CI
// RELATED QA.CODES RESOURCES
Cheat sheets
Glossary
Interview