0

I am trying to create some JUnit tests for my Android app.

In the app I have the following method, which I am trying to get tested:

public void checkBoxAction(View v) { CheckBox cb = (CheckBox) v; Boolean isChecked = cb.isChecked(); //Do stuff } 

I am using Mockito and I have got this far:

// ... CheckBox dummyV = new CheckBox(mMockContext); Mockito.when(dummyV.isChecked()).thenReturn(true); item.checkBoxAction(dummyV); // ... 

But when I run the test I get error:

java.lang.RuntimeException: Method isChecked in android.widget.CompoundButton not mocked... 

I think the problem is that in the method checkBoxAction, the isChecked method is not executed on v, but on other object. Is this assumption correct? Is this the problem? And how can I fix it? Or there is something else?

3
  • Try CheckBox dummyV = mock(CheckBox.class); Commented Jul 6, 2016 at 20:51
  • Yes, this worked! Thanks! Commented Jul 7, 2016 at 10:29
  • ok, added as an answer. If it works please mark it as accepted Commented Jul 7, 2016 at 13:20

1 Answer 1

1

You should mock the CheckBox if you want to use Mockito.when, try:

CheckBox dummyV = mock(CheckBox.class); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.