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
105
votes
11 answers

Square retrofit server mock for testing

What's the best way to mock a server for testing when using the square retrofit framework. Potential ways: Create a new retrofit client and set it in the RestAdapter.Builder().setClient(). This involves parsing the Request object and returning the…
Alec Holmes
  • 3,625
  • 4
  • 22
  • 23
35
votes
6 answers

Simulate no network using Retrofit and MockWebServer

I want to simulate the no network case when using RetroFit and MockWebServer. Im currently testing using Espresso and supplying the MockWebServers url to the RestAdapter before I start my tests. This works great for mocking server responses and so…
Dori
  • 18,283
  • 17
  • 74
  • 116
22
votes
2 answers

How to set MockWebServer port to WebClient in JUnit test?

I'm using spring-boot with WebClient, which is autowired as a bean. Problem: when writing a junit integration test, I have to use okhttp MockWebServer. This mock always starts up on a random port, eg localhost:14321. Now my WebClient of course has a…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
22
votes
1 answer

Why would I use MockWebServer instead of WireMock?

Every time when I need to mock some http requests my first choise is WireMock (which is actually standard for that I think), but today I discovered some alternative tool - MockWebServer. What are pros and cons of WireMock vs MockWebServer?
Paweł Szymczyk
  • 1,638
  • 15
  • 20
21
votes
3 answers

MockWebServer response delay - testing timeouts

I'm trying to test HTTP timeout scenarios using a MockWebServer which answers my test requests sent with Retrofit/OkHttp. (This question was asked before some years ago, but at the time concerned a bug in the MockWebServer. Also, the API has since…
fgysin
  • 11,329
  • 13
  • 61
  • 94
18
votes
1 answer

How can I verify the content of a POST Body with MockWebServer?

I have several unit tests that use Squares OkHttps MockWebServer. The tests run all fine, and everything works very well. The one thing I couldn't do so far is to verify the content of the POST requests. Why do I want to do that? The REST Api I'm…
ASP
  • 773
  • 1
  • 8
  • 22
15
votes
1 answer

How to assign responses in MockWebServer to expected requests?

I want to create an integration test and mock a remote webservice as follows: MockWebServer server = new MockWebServer(); server.enqueue(new MockResponse()...); server.enqueue(new MockResponse()...); server.enqueue(new MockResponse()...); Problem:…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
15
votes
1 answer

How to make android:usesCleartextTraffic="true" only for instrumentation tests?

I'm using RESTMock for my instrumentation tests, but it only works if I set usesCleartextTraffic to true in my manifest. I only want that to be true for instrumentation tests, though. Is there a way to do that? I tried creating a new manifest file…
15
votes
2 answers

MockWebServer's takeRequest() method takes long to response or hangs

I'm using MockWebServer library in my Android JUnit tests. I'm testing an SDK that makes calls to a server. So I'm using MockWebServer to override these server URLs and capture what the SDK is sending to make assertions on it. The problem that I'm…
FilmiHero
  • 2,306
  • 7
  • 31
  • 46
14
votes
1 answer

Test with consecutive calls to MockWebServer

I am writing test for an Activity which makes several consecutive calls to server. My MockWebServer mixes sequence of responses.e.g. When I make two consecutive requests request1 and request2 it sometimes returns request2's Json in response to…
Mag Hakobyan
  • 510
  • 1
  • 4
  • 17
14
votes
4 answers

How to use SSL in Square MockWebServer?

I try to enable SSL on Square's MockWebServer to mock all webservice calls in my Android App under Test. I want to enable SSL to get the same errors like under real conditions. I don't want to disable SSL for the tests or implement a SSL-ignoring…
StefanTo
  • 971
  • 1
  • 10
  • 28
13
votes
2 answers

OkHttp MockWebServer fails to accept connections when restarted

I'm using the OkHttp MockWebServer to mock my server responses for unit tests. It works great for the first test, but on the 2nd test my client fails with: Failed to connect to localhost/0:0:0:0:0:0:0:1:63631 This happens even if the 2nd test is…
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
12
votes
3 answers

MockWebServer and Retrofit with Callback

I would like to simulate network communication by MockWebServer. Unfortulatelly retrofit callbacks are never invoking. My code: MockWebServer server = new MockWebServer(); server.enqueue(new…
lukjar
  • 7,220
  • 2
  • 31
  • 40
12
votes
3 answers

Mock HttpResponse with Robolectric

Using Robolectric 2.3-SNAPSHOT, I want to test an object that'll execute a request in the background. In order to isolate it, I'm trying to mock the HttpResponse returned, without success after some hours invested. I've created a project that anyone…
Maragues
  • 37,861
  • 14
  • 95
  • 96
11
votes
1 answer

MockWebServer: llegalStateException: start() already called

I try to run a test with MockWebServer. I would like to make a UI test with mocked response, so I could test for valid\invalid UI changes like logging in, or showing error in a login API. However, each and every time I ran the code I got the…
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
1
2 3
10 11