For a unit tests, you should be able to write your code in such a way as to isolate the usage of OkHttp. In doing this, you can mock out the client (using something like Mockito) and have it behave however you want. After all, in a unit test we only want to test a single piece of code, not any of it's dependencies.
Something like this in Java. Likewise, the pattern works elsewhere (like Javascript)
public class ClassUnderTest {
private final OkHttpClient client;
public ClassUnderTest(OkHttpClient client) {
this.client = client;
}
// Other code...
}
// In your tests
OkHttpClient mockClient = mock(OkHttpClient.class);
when(mockClient.newCall(any(Request.class)).thenThrow(...);
ClassUnderTest testClass = new ClassUnderTest(mockClient);
verify(mockClient).doSomething();
For integration tests, mockwebserver looks to be your best bet (as others have mentioned).