Migrate react-native Android tests to Robolectric v4 (#28927)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/28927

Changelog:
[Android] [Changed] Test Modernization

Reviewed By: mdvacca

Differential Revision: D21598788

fbshipit-source-id: 6fd9c30220d6d69cc68f13bf1f5ad5a4bc2b039a
This commit is contained in:
Joshua Selbo
2020-05-26 20:38:43 -07:00
committed by Facebook GitHub Bot
parent 6f75065d82
commit 6a78b32878
27 changed files with 472 additions and 346 deletions
@@ -6,7 +6,6 @@ rn_robolectric_test(
contacts = ["oncall+react_native@xmail.facebook.com"],
deps = [
YOGA_TARGET,
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("third-party/android/androidx:annotation"),
react_native_dep("third-party/android/androidx:core"),
react_native_dep("third-party/android/androidx:fragment"),
@@ -15,10 +14,8 @@ rn_robolectric_test(
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/okhttp:okhttp3"),
react_native_dep("third-party/java/okio:okio"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react:react"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
@@ -9,12 +9,9 @@ rn_robolectric_test(
"PUBLIC",
],
deps = [
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react:react"),
react_native_target("java/com/facebook/react/animated:animated"),
react_native_target("java/com/facebook/react/bridge:bridge"),
@@ -1,4 +1,4 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "react_native_tests_target", "rn_android_library", "rn_robolectric_test")
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_android_toplevel_dep", "react_native_dep", "react_native_target", "react_native_tests_target", "rn_android_library", "rn_robolectric_test")
STANDARD_TEST_SRCS = [
"*Test.java",
@@ -14,8 +14,8 @@ rn_android_library(
"PUBLIC",
],
deps = [
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_android_toplevel_dep("third-party/java/mockito2:mockito2"),
react_native_dep("third-party/java/robolectric/4.3.1:robolectric"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_tests_target("java/org/mockito/configuration:configuration"),
@@ -31,13 +31,10 @@ rn_robolectric_test(
],
deps = [
":testhelpers",
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
@@ -7,13 +7,14 @@
package com.facebook.react.bridge;
import static org.mockito.Mockito.when;
import com.facebook.soloader.SoLoader;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -56,14 +57,14 @@ public class BaseJavaModuleTest {
@Test(expected = NativeArgumentsParseException.class)
public void testCallMethodWithoutEnoughArgs() throws Exception {
int methodId = findMethod("regularMethod", mMethods);
Mockito.stub(mArguments.size()).toReturn(1);
when(mArguments.size()).thenReturn(1);
mWrapper.invoke(methodId, mArguments);
}
@Test
public void testCallMethodWithEnoughArgs() {
int methodId = findMethod("regularMethod", mMethods);
Mockito.stub(mArguments.size()).toReturn(2);
when(mArguments.size()).thenReturn(2);
mWrapper.invoke(methodId, mArguments);
}
@@ -71,14 +72,14 @@ public class BaseJavaModuleTest {
public void testCallAsyncMethodWithEnoughArgs() {
// Promise block evaluates to 2 args needing to be passed from JS
int methodId = findMethod("asyncMethod", mMethods);
Mockito.stub(mArguments.size()).toReturn(3);
when(mArguments.size()).thenReturn(3);
mWrapper.invoke(methodId, mArguments);
}
@Test
public void testCallSyncMethod() {
int methodId = findMethod("syncMethod", mMethods);
Mockito.stub(mArguments.size()).toReturn(2);
when(mArguments.size()).thenReturn(2);
mWrapper.invoke(methodId, mArguments);
}
@@ -9,14 +9,11 @@ rn_robolectric_test(
"PUBLIC",
],
deps = [
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/okhttp:okhttp3"),
react_native_dep("third-party/java/okio:okio"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react:react"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
@@ -76,7 +76,7 @@ public class JSDebuggerWebSocketClientTest {
client.onMessage(null, ByteString.encodeUtf8("{\"replyID\":0, \"result\":\"OK\"}"));
PowerMockito.verifyPrivate(client, never())
.invoke("triggerRequestSuccess", anyInt(), anyString());
.invoke("triggerRequestSuccess", anyInt(), nullable(String.class));
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
}
@@ -86,7 +86,7 @@ public class JSDebuggerWebSocketClientTest {
client.onMessage(null, "{\"result\":\"OK\"}");
PowerMockito.verifyPrivate(client, never())
.invoke("triggerRequestSuccess", anyInt(), anyString());
.invoke("triggerRequestSuccess", anyInt(), nullable(String.class));
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
}
@@ -96,7 +96,7 @@ public class JSDebuggerWebSocketClientTest {
client.onMessage(null, "{\"replyID\":null, \"result\":\"OK\"}");
PowerMockito.verifyPrivate(client, never())
.invoke("triggerRequestSuccess", anyInt(), anyString());
.invoke("triggerRequestSuccess", anyInt(), nullable(String.class));
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
}
@@ -131,6 +131,7 @@ public class JSDebuggerWebSocketClientTest {
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
client.onMessage(null, "{\"replyID\":0, \"error\":null}");
PowerMockito.verifyPrivate(client).invoke("triggerRequestSuccess", anyInt(), anyString());
PowerMockito.verifyPrivate(client)
.invoke("triggerRequestSuccess", anyInt(), nullable(String.class));
}
}
@@ -9,7 +9,6 @@ rn_robolectric_test(
],
deps = [
YOGA_TARGET,
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("third-party/android/androidx:annotation"),
react_native_dep("third-party/android/androidx:core"),
react_native_dep("third-party/android/androidx:fragment"),
@@ -18,10 +17,8 @@ rn_robolectric_test(
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/okhttp:okhttp3"),
react_native_dep("third-party/java/okio:okio"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react:react"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
@@ -25,7 +25,7 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ActivityController;
import org.robolectric.android.controller.ActivityController;
@RunWith(RobolectricTestRunner.class)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "androidx.*", "android.*"})
@@ -518,9 +518,9 @@ public class NetworkingModuleTest {
/* withCredentials */ false);
// verify RequestBodyPart for image
PowerMockito.verifyStatic(times(1));
PowerMockito.verifyStatic(RequestBodyUtil.class, times(1));
RequestBodyUtil.getFileInputStream(any(ReactContext.class), eq("imageUri"));
PowerMockito.verifyStatic(times(1));
PowerMockito.verifyStatic(RequestBodyUtil.class, times(1));
RequestBodyUtil.create(MediaType.parse("image/jpg"), inputStream);
// verify body
@@ -579,7 +579,7 @@ public class NetworkingModuleTest {
verify(mHttpClient, times(3)).newCall(any(Request.class));
mNetworkingModule.onCatalystInstanceDestroy();
PowerMockito.verifyStatic(times(3));
PowerMockito.verifyStatic(OkHttpCallUtil.class, times(3));
ArgumentCaptor<OkHttpClient> clientArguments = ArgumentCaptor.forClass(OkHttpClient.class);
ArgumentCaptor<Integer> requestIdArguments = ArgumentCaptor.forClass(Integer.class);
OkHttpCallUtil.cancelTag(clientArguments.capture(), requestIdArguments.capture());
@@ -624,7 +624,7 @@ public class NetworkingModuleTest {
verify(mHttpClient, times(3)).newCall(any(Request.class));
mNetworkingModule.abortRequest(requests);
PowerMockito.verifyStatic(times(1));
PowerMockito.verifyStatic(OkHttpCallUtil.class, times(1));
ArgumentCaptor<OkHttpClient> clientArguments = ArgumentCaptor.forClass(OkHttpClient.class);
ArgumentCaptor<Integer> requestIdArguments = ArgumentCaptor.forClass(Integer.class);
OkHttpCallUtil.cancelTag(clientArguments.capture(), requestIdArguments.capture());
@@ -635,7 +635,7 @@ public class NetworkingModuleTest {
// If `cancelTag` would've been called again for the aborted call, we would have had
// `requests + 1` calls.
mNetworkingModule.onCatalystInstanceDestroy();
PowerMockito.verifyStatic(times(requests));
PowerMockito.verifyStatic(OkHttpCallUtil.class, times(requests));
clientArguments = ArgumentCaptor.forClass(OkHttpClient.class);
requestIdArguments = ArgumentCaptor.forClass(Integer.class);
OkHttpCallUtil.cancelTag(clientArguments.capture(), requestIdArguments.capture());
@@ -32,7 +32,8 @@ public class ReactCookieJarContainerTest {
@Test
public void testMissingJar() throws Exception {
ReactCookieJarContainer jarContainer = mock(ReactCookieJarContainer.class);
assertThat(jarContainer.loadForRequest(any(HttpUrl.class)).size()).isEqualTo(0);
assertThat(jarContainer.loadForRequest(HttpUrl.parse("http://example.com")).size())
.isEqualTo(0);
}
@Test
@@ -40,7 +41,8 @@ public class ReactCookieJarContainerTest {
ReactCookieJarContainer jarContainer = mock(ReactCookieJarContainer.class);
List<Cookie> cookies = new ArrayList<>();
when(jarContainer.loadForRequest(any(HttpUrl.class))).thenReturn(cookies);
assertThat(jarContainer.loadForRequest(any(HttpUrl.class)).size()).isEqualTo(0);
assertThat(jarContainer.loadForRequest(HttpUrl.parse("http://example.com")).size())
.isEqualTo(0);
}
@Test
@@ -51,7 +53,8 @@ public class ReactCookieJarContainerTest {
List<Cookie> cookies = new ArrayList<>();
cookies.add(new Cookie.Builder().name("valid").value("valid value").domain("domain").build());
when(cookieJar.loadForRequest(any(HttpUrl.class))).thenReturn(cookies);
assertThat(jarContainer.loadForRequest(any(HttpUrl.class)).size()).isEqualTo(1);
assertThat(jarContainer.loadForRequest(HttpUrl.parse("http://example.com")).size())
.isEqualTo(1);
}
@Test
@@ -62,6 +65,7 @@ public class ReactCookieJarContainerTest {
List<Cookie> cookies = new ArrayList<>();
cookies.add(new Cookie.Builder().name("valid").value("înválíd välūė").domain("domain").build());
when(cookieJar.loadForRequest(any(HttpUrl.class))).thenReturn(cookies);
assertThat(jarContainer.loadForRequest(any(HttpUrl.class)).size()).isEqualTo(0);
assertThat(jarContainer.loadForRequest(HttpUrl.parse("http://example.com")).size())
.isEqualTo(0);
}
}
@@ -9,6 +9,7 @@ package com.facebook.react.modules.share;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.robolectric.Shadows.shadowOf;
import android.app.Activity;
import android.content.Intent;
@@ -17,6 +18,7 @@ import androidx.annotation.Nullable;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.JavaOnlyMap;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactTestHelper;
import com.facebook.react.bridge.WritableMap;
import org.junit.After;
@@ -31,14 +33,23 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.internal.ShadowExtractor;
import org.robolectric.shadows.ShadowApplication;
@PrepareForTest({Arguments.class})
@RunWith(RobolectricTestRunner.class)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "androidx.*", "android.*"})
@PowerMockIgnore({
"org.mockito.*",
"org.robolectric.*",
"androidx.*",
"android.*",
"javax.xml.*",
"org.xml.sax.*",
"org.w3c.dom.*",
"org.springframework.context.*",
"org.apache.log4j.*"
})
public class ShareModuleTest {
private Activity mActivity;
@@ -58,7 +69,11 @@ public class ShareModuleTest {
}
});
mShareModule = new ShareModule(ReactTestHelper.createCatalystContextForTest());
mActivity = Robolectric.setupActivity(Activity.class);
ReactApplicationContext applicationContext = ReactTestHelper.createCatalystContextForTest();
applicationContext.onNewIntent(mActivity, new Intent());
mShareModule = new ShareModule(applicationContext);
}
@After
@@ -81,9 +96,7 @@ public class ShareModuleTest {
mShareModule.share(content, dialogTitle, promise);
final Intent chooserIntent =
((ShadowApplication) ShadowExtractor.extract(RuntimeEnvironment.application))
.getNextStartedActivity();
final Intent chooserIntent = shadowOf(RuntimeEnvironment.application).getNextStartedActivity();
assertNotNull("Dialog was not displayed", chooserIntent);
assertEquals(Intent.ACTION_CHOOSER, chooserIntent.getAction());
assertEquals(dialogTitle, chooserIntent.getExtras().get(Intent.EXTRA_TITLE));
@@ -36,7 +36,7 @@ import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.concurrent.RoboExecutorService;
import org.robolectric.android.util.concurrent.RoboExecutorService;
/** Tests for {@link com.facebook.react.modules.storage.AsyncStorageModule}. */
@PrepareForTest({Arguments.class})
@@ -9,14 +9,11 @@ rn_robolectric_test(
"PUBLIC",
],
deps = [
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/okhttp:okhttp3"),
react_native_dep("third-party/java/okio:okio"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react/packagerconnection:packagerconnection"),
],
)
@@ -17,7 +17,6 @@ rn_robolectric_test(
],
deps = [
YOGA_TARGET,
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("third-party/android/androidx:annotation"),
react_native_dep("third-party/android/androidx:core"),
react_native_dep("third-party/android/androidx:fragment"),
@@ -26,10 +25,8 @@ rn_robolectric_test(
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/okhttp:okhttp3"),
react_native_dep("third-party/java/okio:okio"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react:react"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
@@ -11,7 +11,6 @@ rn_robolectric_test(
YOGA_TARGET,
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
],
)
@@ -7,12 +7,9 @@ rn_robolectric_test(
"PUBLIC",
],
deps = [
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react/util:util"),
react_native_target("java/com/facebook/react/bridge:bridge"),
],
@@ -9,7 +9,6 @@ rn_robolectric_test(
contacts = ["oncall+fbandroid_sheriff@xmail.facebook.com"],
deps = [
YOGA_TARGET,
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("libraries/fresco/fresco-react-native:fresco-drawee"),
react_native_dep("libraries/fresco/fresco-react-native:fresco-react-native"),
react_native_dep("libraries/fresco/fresco-react-native:imagepipeline"),
@@ -22,10 +21,8 @@ rn_robolectric_test(
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/okhttp:okhttp3"),
react_native_dep("third-party/java/okio:okio"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react:react"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
@@ -1,4 +1,4 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "rn_android_library")
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_android_toplevel_dep", "rn_android_library")
rn_android_library(
name = "configuration",
@@ -7,6 +7,6 @@ rn_android_library(
"PUBLIC",
],
deps = [
react_native_dep("third-party/java/mockito:mockito"),
react_native_android_toplevel_dep("third-party/java/mockito2:mockito2"),
],
)