2

我无法让 Roboelectric 运行我的测试,因为它无法为 AutofillManager 类创建阴影对象。

@RunWith(RobolectricTestRunner.class)
@org.robolectric.annotation.Config(constants = BuildConfig.class)
public class ConnectivityManagerTest
{
private ConnectivityManager connectivityManager;
private ShadowNetworkInfo shadowOfActiveNetworkInfo;
private ShadowConnectivityManager shadowConnectivityManager;

@Before
public void setUp() throws Exception {
    ConnectivityManager connectivityManager = (ConnectivityManager) ShadowApplication
            .getInstance().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    shadowConnectivityManager = shadowOf(connectivityManager);
    shadowOfActiveNetworkInfo = shadowOf(connectivityManager.getActiveNetworkInfo());
}

   @Test
    public void getActiveNetworkInfo_shouldInitializeItself() {
    assertNotNull(shadowOfActiveNetworkInfo);
    }

}

我尝试使用模拟对象并使用 RobolectricTestRunner 运行。测试成功运行。但是看起来创建的阴影对象无法访问 AutofillManager。有什么线索吗?

4

1 回答 1

0

根据对 Robolectric 问题的评论,compileSdkVersion 需要至少为 27(https://github.com/robolectric/robolectric/issues/3828#issuecomment-377586971)。这对我有用。

于 2018-08-26T17:23:33.780 回答