[android] - allow resetting a custom OkHttp client to the default implementation

This commit is contained in:
tobrun
2019-04-05 10:49:57 +02:00
committed by Tobrun
parent 8f331f5ae3
commit 744ce2c026
5 changed files with 87 additions and 32 deletions

View File

@@ -0,0 +1,29 @@
package com.mapbox.mapboxsdk;
import android.content.Context;
import java.lang.reflect.Field;
public class MapboxInjector {
public static void inject(Context context, String accessToken) {
Mapbox mapbox = new Mapbox(context, accessToken);
try {
Field field = Mapbox.class.getDeclaredField("INSTANCE");
field.setAccessible(true);
field.set(mapbox, mapbox);
} catch (Exception exception) {
throw new AssertionError();
}
}
public static void clear() {
try {
Field field = Mapbox.class.getDeclaredField("INSTANCE");
field.setAccessible(true);
field.set(field, null);
} catch (Exception exception) {
throw new AssertionError();
}
}
}

View File

@@ -1,13 +1,10 @@
package com.mapbox.mapboxsdk;
import android.content.Context;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Field;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertSame;
import static org.junit.Assert.assertEquals;
@@ -32,13 +29,13 @@ public class MapboxTest {
@Test
public void testGetAccessToken() {
final String accessToken = "pk.0000000001";
injectMapboxSingleton(accessToken);
MapboxInjector.inject(context, accessToken);
assertSame(accessToken, Mapbox.getAccessToken());
}
@Test
public void testApplicationContext() {
injectMapboxSingleton("pk.0000000001");
MapboxInjector.inject(context, "pk.0000000001");
assertNotNull(Mapbox.getApplicationContext());
assertNotEquals(context, appContext);
assertEquals(appContext, appContext);
@@ -71,27 +68,7 @@ public class MapboxTest {
@After
public void after() {
clearMapboxSingleton();
MapboxInjector.clear();
}
private void injectMapboxSingleton(String accessToken) {
Mapbox mapbox = new Mapbox(context, accessToken);
try {
Field field = Mapbox.class.getDeclaredField("INSTANCE");
field.setAccessible(true);
field.set(mapbox, mapbox);
} catch (Exception exception) {
throw new AssertionError();
}
}
private void clearMapboxSingleton() {
try {
Field field = Mapbox.class.getDeclaredField("INSTANCE");
field.setAccessible(true);
field.set(field, null);
} catch (Exception exception) {
throw new AssertionError();
}
}
}

View File

@@ -0,0 +1,35 @@
package com.mapbox.mapboxsdk.module.http
import com.mapbox.mapboxsdk.MapboxInjector
import io.mockk.mockk
import junit.framework.Assert.assertEquals
import okhttp3.OkHttpClient
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class HttpRequestUtilTest {
@Test
fun replaceHttpClient() {
MapboxInjector.inject(mockk(relaxed = true), "")
assertEquals(HttpRequestImpl.DEFAULT_CLIENT, HttpRequestImpl.client)
val httpMock = mockk<OkHttpClient>()
HttpRequestUtil.setOkHttpClient(httpMock)
assertEquals("Http client should have set to the mocked client",
httpMock,
HttpRequestImpl.client
)
HttpRequestUtil.setOkHttpClient(null)
assertEquals("Http client should have been reset to the default client",
HttpRequestImpl.DEFAULT_CLIENT,
HttpRequestImpl.client
)
MapboxInjector.clear()
}
}