Questions tagged [mockwebserver]

A library for testing HTTP and HTTPS clients.

This library makes it possible to write fast, reliable unit tests for HTTP clients. Like object mocking frameworks, it allows you to script mock responses, exercise them, and then review the recorded requests.

Link: https://code.google.com/p/mockwebserver/

157 questions
6
votes
3 answers

delay MockWebServer response

How can I delay a Square MockWebServer HTTP response? There is a response.setBodyDelayTimeMs(...); method but this is only used in SpdySocketHandler and not for HTTP requests.
Ralph Bergmann
  • 3,015
  • 4
  • 30
  • 61
5
votes
0 answers

OkHttp MockWebServer as standalone mock server

I already have OkHttp MockWebServer running with my Espresso UI tests for Android. Everything is working fine. Now I want to have MockWebServer standalone running on my localhost to other clients to be able to connect to it. Clients like Appium,…
Warlock
  • 2,481
  • 4
  • 31
  • 45
5
votes
1 answer

new MockWebServer fails with java.lang.NoClassDefFoundError ( TaskRunner$RealBackend )

I am trying to use okhttp3.mockwebserver.MockWebServer for some intrumented testing on Android. This is what I have in my build.gradle (:app) file for instrumented testing: androidTestImplementation 'androidx.annotation:annotation:1.1.0' …
Nick
  • 1,118
  • 9
  • 14
5
votes
1 answer

how to mock okhttp response for JUnit test

I'm making an outbound HTTP request to a 3rd party API via okhttp: public @Nullable result Call3rdParty { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) …
Bryan
  • 105
  • 1
  • 8
5
votes
2 answers

OkHttp MockWebServer with dynamic URLs using Retrofit

My app uses dynamic URLs to make web-service calls (on Android). baseUrl is set as empty and we pass Retrofit2 @Url parameters in the service interface: public interface UserService { @GET public Call profilePicture(@Url…
dev
  • 11,071
  • 22
  • 74
  • 122
5
votes
1 answer

Android MockWebServer dispatcher problems

In my MockWebServer I'm using two different dispatcher. One LocalResponseDispatcher for locally add response and a normal QueueDispatcher in order to enqueue some stub responses. I had problem to switch between twos. Infact when I'm trying to use…
5
votes
1 answer

How to support multiple endpoints with Dagger 2.0 while using MockWebServer

For an Android project I configured Retrofit 2.1.0 and OkHttp 3.4.1 with Dagger 2.6 as shown in the following Dagger module. I am aiming to support multiple backends using @Named qualifiers. @Module public class ApiModule { private static final…
JJD
  • 50,076
  • 60
  • 203
  • 339
5
votes
0 answers

Mockwebserver where url cannot be changed?

I am using mockwebserver for android testing. I am able to mock my network services that uses retrofit by doing NetworkServiceHelper.init(String.valueOf(server.url("/"))); However, I am unable to mock request for web request made by other SDKs…
Prabin Timsina
  • 2,131
  • 3
  • 16
  • 27
5
votes
1 answer

Mocking server results on Android (Wiremock, MockWebServer etc)

Does anyone have a full and recent tutorial or project on recording server responses- get and post and headers as needed AND Playing them back with wiremock and/or mockwebserver? I've looked at many out there already
nAndroid
  • 862
  • 1
  • 10
  • 25
4
votes
2 answers

TimeoutCancellationException when running tests for a Retrofit coroutine in a Kotlin flow

I have a repository that creates a flow where I emit the result of a suspending Retrofit method. This works in the app, but I would like to run tests on the code. I am using kotlinx-coroutines-test v1.6.0 and MockWebServer v4.9.3 in my tests. When I…
Eva
  • 4,397
  • 5
  • 43
  • 65
4
votes
1 answer

Using Okhttp.MockebServer for testing websocket

I am using okhttp.MockWebServer to test rest api calls. Is there a way to use it for mocking a websocket call as well? Essentially, I have a ws client (Client extends WebSocketListener) which calls an external websocket server and gets back…
CaptainHastings
  • 1,557
  • 1
  • 15
  • 32
4
votes
1 answer

How Can I Mock A Specific URL Path?

I am using okhttp to mock my http responses during my tests. //Create a mock server mockWebServer.start(8080) mockWebServer.enqueue(MockResponse().setBody("").setResponseCode(HttpURLConnection.HTTP_OK)) However, this responds to every path as…
opticyclic
  • 7,412
  • 12
  • 81
  • 155
4
votes
1 answer

How can I use MockWebServer for Junit test cases of webclient?

I have a spring-boot application which calls some third party URL (let's say http://example.com/someUri) using webclient(I have used application-dev.properties for injecting this url in my application to achieve loose coupling) and consumes the…
4
votes
2 answers

trying to test with MocWebSerevr it got error

Anyone knows what the error means? Or maybe miss something? Was trying to start from this sample, added the dependency: testImplementation 'com.squareup.okhttp3:mockwebserver:3.11.0' code was just added a @Test…
lannyf
  • 9,865
  • 12
  • 70
  • 152
4
votes
1 answer

OkHttp MockWebServer fails to accept connections in android P (API 28) Instrumentation Test

When I have updated my sdk version from API 26 to API 28, OKHttp mockserver fails to run in the device/emulator in my android instrumentation test. It ran perfectly in the older versions. I have checked in the Network Config…
Prabhakaran
  • 1,264
  • 2
  • 20
  • 47
1 2
3
10 11