鉴于我正在使用这样的AndroidAnnotations:
var myCalendar: EKCalendar?
let calendars = eventStore.calendars(for: .reminder)
if let filteredCalendar = calendars.first {$0.title == "MyNewCalendar"} {
myCalendar = filteredCalendar
} else {
print("count not find reminder calendar 'MyNewCalendar'")
}
然后,生成的@EFragment(R.layout.fragment_music_home2)
public class MusicHomeFragment extends AppsBasedHomeFragment {
@ViewById
protected ExtRecyclerView pager;
}
代码在某一点看起来像这样:
MusicHomeFragment_
这导致堆栈跟踪如下:
pager = ((ExtRecyclerView) hasViews.findViewById(com.company.R.id.pager));
看看测试的样子:
java.lang.ClassCastException: com.company.view.ExtRecyclerView_ cannot be cast to com.company.view.ExtRecyclerView
at com.company.music.MusicHomeFragment_.onViewChanged(MusicHomeFragment_.java:97)
at org.androidannotations.api.view.OnViewChangedNotifier.notifyViewChanged(OnViewChangedNotifier.java:41)
at com.company.music.MusicHomeFragment_.onViewCreated(MusicHomeFragment_.java:85)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1120)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1929)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at com.company.utils.BaseActivity.onStart(BaseActivity.java:104)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5253)
at org.robolectric.util.ReflectionHelpers$3.run(ReflectionHelpers.java:162)
at org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:305)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:156)
at org.robolectric.util.ComponentController$1.run(ComponentController.java:72)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
at org.robolectric.util.ComponentController.invokeWhilePaused(ComponentController.java:69)
at org.robolectric.util.ActivityController.start(ActivityController.java:143)
at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
at com.company.music.MusicHomeFragmentTest.showEmptyPageWhenNoContentsAreAvailable(MusicHomeFragmentTest.java:65)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:52)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:638)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
的build.gradle:
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@PrepareForTest(MyThemeManager_.class)
public class MusicHomeFragmentTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Test
public void showEmptyPageWhenNoContentsAreAvailable() {
mockStatic(MyThemeManager_.class);
final MyThemeManager_ themeManager = mock(MyThemeManager_.class);
when(MyThemeManager_.getInstance_(Mockito.<Context>anyObject())).thenReturn(themeManager);
MusicHomeActivity_ controller = Robolectric.setupActivity(MusicHomeActivity_.class);
// TODO: Start the test.
PowerMockito.verifyStatic();
}
}
如何解决这个问题,以便测试可以完美无缺地执行?
答案 0 :(得分:1)
需要使用针对错误转换类的@PowerMockIgnore规则来增强测试。它似乎被模拟框架的组合所覆盖。
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
// ** CHANGE BELOW **
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "com.company.view.ExtRecyclerView_"})
@PrepareForTest(MyThemeManager_.class)
public class MusicHomeFragmentTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Test
public void showEmptyPageWhenNoContentsAreAvailable() {
mockStatic(MyThemeManager_.class);
final MyThemeManager_ themeManager = mock(MyThemeManager_.class);
when(MyThemeManager_.getInstance_(Mockito.<Context>anyObject())).thenReturn(themeManager);
MusicHomeActivity_ controller = Robolectric.setupActivity(MusicHomeActivity_.class);
// TODO: Start the test.
PowerMockito.verifyStatic();
}
}