Search code examples
androidmockwebserver

Is there a way to fix the port for MockWebServer


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.


Solution

  • 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.