使用 Robolectric ClassCastException 进行 Android 测试:android.app.Application 无法转换为 MyApplication
Posted
技术标签:
【中文标题】使用 Robolectric ClassCastException 进行 Android 测试:android.app.Application 无法转换为 MyApplication【英文标题】:Android Testing with Robolectric ClassCastException: android.app.Application cannot be cast to MyApplication 【发布时间】:2014-07-04 06:54:33 【问题描述】:我正在使用 Robolectric 测试我的 android 应用程序,但我正在努力解决这个问题:
MyModel myModel = ((MyApplication) context.getApplicationContext()).getMyModel;
显然 Robolectric 正在抱怨这个错误:
java.lang.RuntimeException: java.lang.ClassCastException: android.app.Application cannot be cast to com.reply.delhaize.common.DelhaizeApplication
at org.robolectric.util.SimpleFuture.run(SimpleFuture.java:57)
at org.robolectric.shadows.ShadowAsyncTask$2.run(ShadowAsyncTask.java:95)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:37)
at org.robolectric.util.Scheduler.post(Scheduler.java:42)
at org.robolectric.shadows.ShadowAsyncTask.execute(ShadowAsyncTask.java:92)
at android.os.AsyncTask.execute(AsyncTask.java)
at com.reply.delhaize.common.models.StoreModel.getStoreList(StoreModel.java:617)
at com.reply.delhaize.common.models.StoreModel.fetchAllStores(StoreModel.java:107)
at com.reply.delhaize.core.tests.storefinder.StoreFinderTest.fetchAllStoresWhenWifiIsOn(StoreFinderTest.java:84)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.reply.delhaize.common.DelhaizeApplication
at com.my.package.name.models.GetApiModelTaskOtto.doInBackground(GetApiModelTaskOtto.java:74)
at com.my.package.name.models.GetApiModelTaskOtto.doInBackground(GetApiModelTaskOtto.java:1)
at android.os.ShadowAsyncTaskBridge.doInBackground(ShadowAsyncTaskBridge.java:14)
at org.robolectric.shadows.ShadowAsyncTask$BackgroundWorker.call(ShadowAsyncTask.java:149)
at org.robolectric.util.SimpleFuture.run(SimpleFuture.java:52)
at org.robolectric.shadows.ShadowAsyncTask$2.run(ShadowAsyncTask.java:95)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:37)
at org.robolectric.util.Scheduler.post(Scheduler.java:42)
at org.robolectric.shadows.ShadowAsyncTask.execute(ShadowAsyncTask.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.robolectric.bytecode.ShadowWrangler$ShadowMethodPlan.run(ShadowWrangler.java:456)
at android.os.AsyncTask.execute(AsyncTask.java)
at com.my.package.name.models.StoreModel.getStoreList(StoreModel.java:617)
at com.my.package.name.models.StoreModel.fetchAllStores(StoreModel.java:107)
at com.reply.delhaize.core.tests.storefinder.StoreFinderTest.fetchAllStoresWhenWifiIsOn(StoreFinderTest.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
... 22 more
我想找到一个合适的方法来测试它。
我已经在清单中声明了 MyApplication:
<application
android:name="com.my.package.name.MyApplication"
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
......
这些是测试
@RunWith(RoboletricTestRunner.class)
public class StoreFinderTest extends BaseTest
StoreListActivity storeListActivity;
StoreModel storeModel;
UserModel userModel;
@Before
public void setUp() throws Exception
storeListActivity = Robolectric.buildActivity(StoreListActivity.class).get();
storeModel = new StoreModel(storeListActivity.getApplicationContext());
userModel = new UserModel(storeListActivity.getApplicationContext());
@Test
public void activityExists()
assertThat(storeListActivity).isNotNull();
@Test
public void fetchAllStoresWhenWifiIsOn()
ConnectivityManager cm =(ConnectivityManager)Robolectric.application.getSystemService(Context.CONNECTIVITY_SERVICE);
Robolectric.shadowOf(cm).setNetworkInfo(ConnectivityManager.TYPE_WIFI, cm.getActiveNetworkInfo());
boolean result = storeModel.fetchAllStores();
assertTrue(result);
storeModel.fetchAllStores() 实现有这个丑陋的演员:
MyModel myModel = ((MyApplication) context.getApplicationContext()).getMyModel;
那么有没有办法用 Robolectric 部分包含这样的强制转换的代码进行测试?
((MyApplication) context.getApplicationContext())
【问题讨论】:
【参考方案1】:您可能需要为测试指定 AndroidManifest.xml 的路径。您可以使用以下方法:
@Config(manifest = "/path/to/your/AndroidManifest.xml")
@RunWith(RoboletricTestRunner.class)
public class StoreFinderTest extends BaseTest
...
您可以让 Robolectric 使用您的应用程序的测试版本,只需在项目的测试目录中与您的 MyApplication 相同的包中创建一个 TestMyApplication 类(即,在您的应用程序类名称前添加前缀“Test”的类) .为了解决您的投射问题,TestMyApplication 应该扩展 MyApplication:
public class TestMyApplication extends MyApplication
【讨论】:
编辑了答案..您可以尝试创建一个测试应用程序类,按照描述扩展您的应用程序类 我尝试创建 TestMyApplication 但它没有解决问题。 这是我的确切问题,我给测试应用程序命名为 MyTestApplication 因为我不知道这个小细节如此重要:/ 通过重命名它解决了问题。谢谢【参考方案2】:遇到同样的错误;
java.lang.ClassCastException: androidx.test.runner.AndroidJUnitRunner cannot be cast to org.robolectric.android.fakes.RoboMonitoringInstrumentation
我意识到我为 Roboelectric 使用了错误的实现:
implementation "org.robolectric:robolectric:4.5.1"
如果您正在使用它,请将其替换为 testImplementation:
testImplementation "org.robolectric:robolectric:$robolectricVersion"
【讨论】:
【参考方案3】:我不知道 Robolectric,但可以添加一个if(context.getApplicationContext() instanceof MyApplication
声明
【讨论】:
以上是关于使用 Robolectric ClassCastException 进行 Android 测试:android.app.Application 无法转换为 MyApplication的主要内容,如果未能解决你的问题,请参考以下文章