![]() It's good at providing pre-defined responses to method calls, but things get challenging. Mockito is the most common mocking library for Java. Use WebClient for real, but mock the service it calls by using MockWebServer (okhttp) 3. To handle the sequence of responses I have written a dispatcher for my MockServer instance. We have two main options for mocking in our tests: Use Mockito to mimic the behavior of WebClient. This sequence doesn't work in case of parallel requests. MockWebServer.start(): starts the mock web server on the local host MockWebServer.enqueue(mockResponse): queues a MockResponse. So you have one server per test and can setDispatcher individually. Enqueue an empty body with response code 401 server.enqueue(httpCode. While it will most likely be possible to find a way to handle all tests in one dispatcher another approach could be to instantiate and close the mockwebserver in BeforeEach instead of BeforeAll. The first request is served by the first enqueued response the second request by the second enqueued response and so on." MockWebServer aims to facility testing by offering a flexible stand alone Server. You can vote up the ones you like or vote down the ones you dont like, and go to the original project or source file by following the links above each example. ![]() These examples are extracted from open source projects. In documentation it is said "Enqueue scripts response to be returned to a request made in sequence. The following examples show how to use (). setBody(readFromFile("response2 path")) setBody(readFromFile("response1 path")) How can I specify which response MockWebServer has to return to specified request? server.enqueue(new MockResponse() When I make two consecutive requests request1 and request2 it sometimes returns request2's Json in response to request1 and request1's Json in response to request2. My MockWebServer mixes sequence of responses.e.g. I am writing test for an Activity which makes several consecutive calls to server.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |