Reputation: 412
I have an app on API 34 that runs MockWebServer for the tests. Mockweberser is triggered only for tests that are mocked. Mockweberser class inits on port 8080
class MockedkWebServer {
...
init {
val mockServer = MockWebServer()
mockServer.start(8080)
...
}
...
}
I am using 5.0.0-alpha.12
dependency version for MockWebServer. Changing port to any other not changing the situation.
It's all working fine for API 34.
When running mocked tests against API 35 device it fails for those tests. MockWebServer on test start fails the test after the 1s and threw an error :
java.net.UnknownHostException: Unable to resolve host "localhost"; No address associated with hostname.
Stacktrace pinpoint to mockServer.start(8080)
invocation.
I have dug a little, and found that after adding some ip's to the local network config.xml file, tests start to work, but may fail at the start, so this is not the solution I need :
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="false">10.0.2.2</domain>
<domain includeSubdomains="false">10.0.2.3</domain> // added
<domain includeSubdomains="false">10.0.3.2</domain>
<domain includeSubdomains="true">10.0.2.15</domain> // added
<domain includeSubdomains="true">10.0.2.16</domain>// added
</domain-config>
Question is - what can cause such behavior ? What ways to fix it.
Upvotes: 0
Views: 38
Reputation: 412
Apparently API 35 / Android 15 have changes that affect how local hostnames (like localhost) are resolved.
Just add InetAddress.getByName("127.0.0.1")
to bind to the loopback address explicitly, to 127.0.0.1
, which is a localhost
IP, like this
mockWebServer.start(InetAddress.getByName("127.0.0.1"), 8080);
After this tests have started to work correct
Upvotes: 0