Skip to content

Commit

Permalink
introduce shadows helper library for robolectric (#36733)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #36733

Changelog:
[Android][Added] - Added testing shadow helpers for robolectric

in this change, i'm creating a centralized place for test writers to add their shadows in robolectric. as we start deprecating powermock, we can expect that common infra classes will be needed to be stubbed out, so we can leverage this library in order to do so.

Reviewed By: javache

Differential Revision: D44565806

fbshipit-source-id: 2e322861e8e2f49ede21e4a000495d5f06b911d3
  • Loading branch information
philIip authored and facebook-github-bot committed Mar 31, 2023
1 parent 47149d2 commit 4890d50
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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")

oncall("react_native")

STANDARD_TEST_SRCS = [
"*Test.java",
]
Expand Down Expand Up @@ -43,5 +45,6 @@ rn_robolectric_test(
react_native_target("java/com/facebook/react/turbomodule/core/interfaces:interfaces"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_tests_target("java/com/facebook/common/logging:logging"),
react_native_tests_target("java/com/facebook/testutils/shadows:shadows"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,15 @@

import static org.mockito.Mockito.when;

import android.content.Context;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import com.facebook.soloader.SoLoader;
import com.facebook.testutils.shadows.ShadowSoLoader;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(SoLoader.class)
class ShadowSoLoader {
@Implementation
public static void init(Context context, int flags) {}

@Implementation
public static boolean loadLibrary(String shortName) {
return true;
}
}

/** Tests for {@link BaseJavaModule} and {@link JavaModuleWrapper} */
@Config(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")

oncall("react_native")

rn_android_library(
name = "shadows",
srcs = glob(["**/*.java"]),
autoglob = False,
language = "JAVA",
visibility = [
"PUBLIC",
],
deps = [
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_dep("third-party/java/robolectric:robolectric"),
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.testutils.shadows;

import android.content.Context;
import com.facebook.soloader.SoLoader;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(SoLoader.class)
public class ShadowSoLoader {
@Implementation
public static void init(Context context, int flags) {}

@Implementation
public static boolean loadLibrary(String shortName) {
return true;
}
}

0 comments on commit 4890d50

Please sign in to comment.