3

I am trying to use MockWebServer for running my Android test cases. I am using the server.url("/") to set the path, but I was trying to figure out if there is any way to fix the port as it constantly changes.

pixelWorld
  • 329
  • 2
  • 17

1 Answers1

5

Yes there should be a way, like calling server.start(port) but I think the documentation says it all:

MockWebServer.start(int)

Starts the server on the loopback interface for the given port.

Parameters: port - the port to listen to, or 0 for any available port. Automated tests should always use port 0 to avoid flakiness when a specific port is unavailable.

vilpe89
  • 4,656
  • 1
  • 29
  • 36
  • Thanks! That worked. The reason for me to fix it, so that I can keep a MOCK URL and the retrofit instance could be reused. Is that the correct approach to use since we are running tests serially? – pixelWorld May 18 '20 at 19:07
  • I have been using a different mock server library in my projects and I don't have any knowledge of MockWebServer so I really cant say. – vilpe89 May 18 '20 at 19:14