MainActivity extends AppCompatActivity { @VisibleForTesting void initViews() { EditText weightText = (EditText)findViewById(R.id.text_weight); EditText heightText = (EditText)findViewById(R.id.text_height); TextView resultText = (TextView)findViewById(R.id.text_result); mCalcButton = (Button)findViewById(R.id.button_calculate); View.OnClickListener buttonListener = createButtonListener(weightText, heightText, resultText); mCalcButton.setOnClickListener(buttonListener); }    }
@Test public void initViewsを呼ぶとボタンのListenerが設定される() { EditText weightText = mock(EditText.class); EditText heightText = mock(EditText.class); TextView resultText = mock(TextView.class); Button resultButton = mock(Button.class); View.OnClickListener buttonListener = mock(View.OnClickListener.class); MainActivity activity = spy(new MainActivity()); when(activity.findViewById(R.id.text_weight)).thenReturn(weightText); when(activity.findViewById(R.id.text_height)).thenReturn(heightText); when(activity.findViewById(R.id.text_result)).thenReturn(resultText); when(activity.findViewById(R.id.button_calculate)).thenReturn(resultButton); when(activity.createButtonListener(weightText, heightText, resultText)).thenReturn(buttonListener); activity.initViews(); verify(activity, times(1)).createButtonListener(weightText, heightText, resultText); verify(resultButton, times(1)).setOnClickListener(buttonListener); }