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
2
votes
1 answer

How can I send a streamed response using OkHttp's mockwebserver?

The typical flow when returning the contents of file from a server back to the client are to: 1.) Obtain an inputstream to the file 2.) Write chunks of the stream to the open socket 3.) Close the input stream When using OkHttp's mockwebserver the…
William Seemann
  • 3,440
  • 10
  • 44
  • 78
2
votes
2 answers

How to trigger IOException in response.body().string() with MockWebServer

I have this retrofit call: api.enqueue(new Callback() { @Override public void onFailure(Call call, Throwable t) { t.printStackTrace(); } @Override public void onResponse(Call
CarHa
  • 1,148
  • 11
  • 31
2
votes
1 answer

Unable to read Json File using a MockServerResponse

I have been working with Retrofit and I am trying to Unit test the code. So I tried to create a mock Json file called success_response.json and failed_response.json and saved it inside a resource folder in my project. When I test below code, it…
2
votes
1 answer

unresolved supertypes: org.junit.rules.ExternalResource when using MockWebServer with Junit 5

I'm using MockWebServer from okhttp3 dependencies { testImplementation "org.junit.jupiter:junit-jupiter-api:5.5.2" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.5.2" testImplementation…
kingston
  • 11,053
  • 14
  • 62
  • 116
2
votes
1 answer

Getting "reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response" while mocking a third party API

My Spring boot API is consuming a third part API using WebClient DemoAPIController @RestController public class DemoAPIController { @Autowired DemoService demoService; @GetMapping("/mytest") public Mono getUserType() { …
2
votes
1 answer

Mock API calls made in Application class as part of UI testing

Background: Our Android Application, onCreate, launches some initialisation use cases that execute some network API calls. In parallel, our LAUNCHER Activity is launched. In our UI Tests, we use MockWebServer to stub API responses and we use…
2
votes
0 answers

cannot access ExternalResource when calling MockWebServer

I am having trouble getting MockWebServer working. I added the dependency to build.gradle testImplementation 'com.squareup.okhttp3:mockwebserver:4.2.0' I have the following class (I copied from here…
2
votes
1 answer

MockWebServer - ConnectException: Failed to connect to

When I start my test I get connect exception and I have no idea why. I found some code others authors and compare code and i didn't find anything wrong. I tried to change ports or remove it and it didn't help me. @RunWith(AndroidJUnit4::class) class…
Arkhipov Pavel
  • 68
  • 1
  • 10
2
votes
1 answer

How to set default response for MockWebServer?

MockWebServer is awesome library, but there is one thing that is surprisingly hard to do: set default response. To be specific: I want to have ability to set response that is returned if no response was specified using server.enqueue(response). I…
michalbrz
  • 3,354
  • 1
  • 30
  • 41
2
votes
0 answers

Instrumentation Tests faild due to NoClassDefFoundError

I'm multidexing my kotlin app since today in the debug mode because a library has forced me to do so. My problem is that my UI-Tests aren't working anymore. I'm also minifying my apk. I'm getting the following error: FATAL EXCEPTION:…
finki
  • 2,063
  • 1
  • 20
  • 23
2
votes
0 answers

Unable to set multiple expectation with MockWebServiceServer

I am using a MockWebServiceServer to test the REST APIs. I am passing the values to it using @Runwith(Parameterized.class). @RunWith(Parameterized.class) public class MyAPITest { protected static MockWebServiceServer mockServer; …
rupesh
  • 413
  • 9
  • 19
2
votes
1 answer

Can MockWebServer use local Json File as Response body?

How can I use local Json File as response body in MockWebServer? Thank you!
LunarS
  • 95
  • 2
  • 10
1
vote
0 answers

How do you mock an error response with MockWebServer while using retryWhen?

I am in the process of writing unit tests for a reactive API. I am using spring-boot 2.7.8 and mockwebserver 4.10.0. The method under test simply calls a third-party API with WebClient. I am having trouble writing the test for an error case. The…
D N
  • 11
  • 1
1
vote
0 answers

Android MockWebServer Dispatch not being called

I'm trying to use MockWebServer in my instrumented tests but its not working. The real API is being called, and not the mock itself. com.squareup.okhttp3:mockwebserver:4.10.0 I have my base class: abstract class BaseInstrumentedTest { private…
LMaker
  • 1,444
  • 3
  • 25
  • 38
1
vote
0 answers

WebClient Integration Test with MockWebServer

I dont know if my configuration is wrong or something with mockwebserver. I have method in one of my services public UserResponse getUsername(String username) { return webClient.get() .uri("http://user-service/api/user", …