我有一个RecyclerView
和几个CardView
。我使用以下代码将RelativeLayout
张卡片的背景设置为:
layout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.item_background_grey));
具有不同可绘制对象的卡片看起来像
我想通过浓缩咖啡测试检查可绘制对象。
我得到ViewInteraction
这样的话:
ViewInteraction relativeLayout2 = onView(
allOf(withId(R.id.last_interaction_entry_layout),
childAtPosition(
childAtPosition(
withId(R.id.tab_last_interaction_recyclerview),
1),
0),
isDisplayed()));
当我检查里面的文字时,它可以正常工作。但是当我检查背景时
relativeLayout2.check(matches(hasBackground(R.drawable.item_background_grey)));
它给我以下错误:
androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has background with drawable ID: 2131165305' doesn't match the selected view.
Expected: has background with drawable ID: 2131165305
Got: "RelativeLayout{id=2131230886, res-name=last_interaction_entry_layout, visibility=VISIBLE, width=736, height=113, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@2242239, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}"
当我尝试检查是否有具有这样背景的任何视图
onView(allOf(hasBackground(R.drawable.item_background)))
.check(matches(isDisplayed()));
我得到NoMatchingViewException
。
还尝试像建议的here,here和there一样直接检查颜色,得到了
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.ColorDrawable.getColor()' on a null object reference