我有一个特殊的需要写一个测试如下:
@RunWith(RobolectricTestRunner.class)
public class LongClickTest {
@Test
public void testPerformLongClick() {
View view = new View(Robolectric.application);
view.performLongClick();
}
}但它给我带来了以下错误:
java.lang.NullPointerException
at android.view.View.showContextMenu(View.java:4154)
at android.view.View.performLongClick(View.java:4123)
at org.robobinding.widget.view.OnLongClickAttributeTest.longClickOnView(OnLongClickAttributeTest.java:34)
at org.robobinding.widget.view.OnLongClickAttributeTest.givenBoundAttribute_whenLongClickOnView_thenEventReceived(OnLongClickAttributeTest.java:27)
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:250)
...我检查了一个类似的测试,在org.robolectric.shadows.ViewTest.performLongClick_shouldClickOnView,本身,但我不知道为什么在罗波利克作品和我的没有。顺便说一下,我用的是Robolector2.3。
感谢你的帮助!
谢谢你,程
发布于 2020-03-20 02:57:33
我正在测试上下文菜单。它是从listView的一行调用的。我就是这么做的(在科特林):
val activity = buildActivity(MyActivity::class.java).create().get()
val customView = LayoutInflater.from(activity)
.inflate(R.layout.custom_xml, activity.my_list_view, false)
as MyCustomView
val linearLayout = activity.my_list_view.parent as LinearLayout
linearLayout.addView(customView)现在,performClick工作了。我只需要学习如何获得de上下文菜单来测试这个!
https://stackoverflow.com/questions/25880746
复制相似问题