I'm having trouble to create a unit test for my custom view. I try to add an attribute and test it if my custom view class get it right.
Here's what my test looks like:
@RunWith(AndroidJUnit4.class)
@SmallTest
public class BaseRatingBarMinRatingTest {
private Context mContext;
@Before
public void setUp(){
mContext = InstrumentationRegistry.getTargetContext();
}
@Test
public void constructor_should_setMinRating_when_attriSetHasOne() throws Exception{
// 1. ARRANGE DATA
float minRating = 2.5f;
AttributeSet as = mock(AttributeSet.class);
when(as.getAttributeFloatValue(eq(R.styleable.BaseRatingBar_srb_minRating), anyFloat())).thenReturn(minRating);
// 2. ACT
BaseRatingBar brb = new BaseRatingBar(mContext, as);
// 3. ASSERT
assertThat(brb.getMinRating(), is(minRating));
}
// ...
}
Which gets this Exception:
java.lang.ClassCastException: android.util.AttributeSet$MockitoMock$1142631110 cannot be cast to android.content.res.XmlBlock$Parser
I tried mocking TypeArray like this article did, but my view treats the mocked context as null.
Is there any good way to create a test case for custom view?