我试图模拟Firebase Analytics for Android单元测试的实例,但在我调用Firebase的getInstance()
方法后,我的Firebase变量仍未定义 - 这应该初始化FirebaseAnalytics的一个实例类。
我正在使用PowerMock和Android Studio。这是我的Test.java文件(还没有测试 - 只是尝试初始化类的模拟):
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 18, manifest =
Config.NONE)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest({FirebaseAnalytics.class})
public class FirebaseTest {
@Rule public PowerMockRule rule = new PowerMockRule();
@Mock Application application;
@Mock Analytics analytics;
FirebaseAnalytics firebase;
@Before
public void setUp() {
initMocks(this);
/* mock context */
when(analytics.getApplication()).thenReturn(application);
PowerMockito.mockStatic(FirebaseAnalytics.class);
when(FirebaseAnalytics.getInstance(application)).thenReturn(firebase); /* firebase remains null */
PowerMockito.mockStatic(FirebaseAnalytics.class);
}
FirebaseAnalytics是一个final类,getInstance()是一个静态的final方法。我不确定这些是否会使嘲弄变得困难。任何想法都表示赞赏!