SaveBmiService extends IntentService { @Override protected void onHandleIntent(Intent intent) { if (intent == null) { return; } Serializable extra = intent.getSerializableExtra(PARAM_KEY_BMI_VALUE); if (extra == null || !(extra instanceof BmiValue)) { return; } BmiValue bmiValue = (BmiValue)extra; boolean result = saveToRemoteServer(bmiValue); sendLocalBroadcast(result); } SaveBmiService(); @Override void onCreate(); static void start(Context context, BmiValue bmiValue); static final String ACTION_RESULT; static final String PARAM_RESULT; }
@Test public void onHandleIntentにnullを渡したら何もしない() { SaveBmiService service = spy(new SaveBmiService()); service.onHandleIntent(null); verify(service, never()).sendLocalBroadcast(anyBoolean()); verify(service, never()).saveToRemoteServer((BmiValue) any()); }
@Test public void onHandleIntentにパラメータ無しのIntentを渡したら何もしない() { SaveBmiService service = spy(new SaveBmiService()); service.onHandleIntent(mock(Intent.class)); verify(service, never()).sendLocalBroadcast(anyBoolean()); verify(service, never()).saveToRemoteServer((BmiValue)any()); }
@Test public void onHandleIntentにBmiValue型以外のデータが入ったIntentを渡したら何もしない() { Intent intent = mock(Intent.class); when(intent.getSerializableExtra(SaveBmiService.PARAM_KEY_BMI_VALUE)).thenReturn("hoge"); SaveBmiService service = spy(new SaveBmiService()); service.onHandleIntent(intent); verify(service, never()).sendLocalBroadcast(anyBoolean()); verify(service, never()).saveToRemoteServer((BmiValue)any()); }
@Test public void onHandleIntentに正しくデータが入ったIntentを渡せばデータ保存とBroadcastが行われる() { BmiValue bmiValue = mock(BmiValue.class); Intent intent = mock(Intent.class); when(intent.getSerializableExtra(SaveBmiService.PARAM_KEY_BMI_VALUE)).thenReturn(bmiValue); SaveBmiService service = spy(new SaveBmiService()); doReturn(false).when(service).saveToRemoteServer((BmiValue)any()); doNothing().when(service).sendLocalBroadcast(anyBoolean()); service.onHandleIntent(intent); verify(service, times(1)).sendLocalBroadcast(anyBoolean()); verify(service, times(1)).saveToRemoteServer((BmiValue)any()); }