### Question:
PostOrderTask extends Task { @Nullable @Override Void execute() throws Exception { String advertisingId = features.getIncludesIfa() ? deviceManager.getAdvertisingId() : null; while (true) { try { return buttonApi.postOrder(order, applicationId, sourceToken, advertisingId); } catch (ButtonNetworkException exception) { if (!shouldRetry(exception)) { throw exception; } retryCount++; } } } PostOrderTask(@Nullable Listener listener, ButtonApi buttonApi, Order order,
            String applicationId, String sourceToken, DeviceManager deviceManager,
            Features features, ThreadManager threadManager);   }### Answer:
@Test public void execute_networkNotFoundException_verifyMaxRetry() throws Exception { when(buttonApi.postOrder(any(Order.class), anyString(), anyString(), (String) isNull())).thenThrow(NetworkNotFoundException.class); try { task.execute(); } catch (Exception ignored) { } verify(buttonApi, times(MAX_RETRIES + 1)) .postOrder(any(Order.class), anyString(), anyString(), (String) isNull()); }
@Test(expected = NetworkNotFoundException.class) public void execute_networkNotFoundException_verifyThrowException() throws Exception { when(buttonApi.postOrder(any(Order.class), anyString(), anyString(), (String) isNull())).thenThrow(NetworkNotFoundException.class); task.execute(); }
@Test(expected = Exception.class) public void execute_exception_verifyThrowException() throws Exception { when(buttonApi.postOrder(any(Order.class), anyString(), anyString(), (String) isNull())).thenThrow(Exception.class); task.execute(); }
@Test public void execute_includesIfa_hasAdvertisingId_verifyApiCall() throws Exception { when(features.getIncludesIfa()).thenReturn(true); String advertisingId = "valid_advertising_id"; when(deviceManager.getAdvertisingId()).thenReturn(advertisingId); task.execute(); verify(buttonApi).postOrder(order, applicationId, sourceToken, advertisingId); }
@Test public void execute_includesIfa_nullAdvertisingId_verifyNullAdvertisingId() throws Exception { when(features.getIncludesIfa()).thenReturn(true); when(deviceManager.getAdvertisingId()).thenReturn(null); task.execute(); verify(buttonApi).postOrder(eq(order), eq(applicationId), eq(sourceToken), (String) isNull()); }
@Test public void execute_doesNotIncludesIfa_verifyNullAdvertisingId() throws Exception { when(features.getIncludesIfa()).thenReturn(false); when(deviceManager.getAdvertisingId()).thenReturn(""); task.execute(); verify(buttonApi).postOrder(eq(order), eq(applicationId), eq(sourceToken), (String) isNull()); }
@Test public void execute_httpStatusException_wasServerError_verifyMaxRetry() throws Exception { HttpStatusException httpStatusException = mock(HttpStatusException.class); when(httpStatusException.wasServerError()).thenReturn(true); when(buttonApi.postOrder(any(Order.class), anyString(), anyString(), (String) isNull())).thenThrow(httpStatusException); try { task.execute(); } catch (Exception ignored) { } verify(buttonApi, times(MAX_RETRIES + 1)) .postOrder(any(Order.class), anyString(), anyString(), (String) isNull()); }
@Test(expected = HttpStatusException.class) public void execute_httpStatusException_wasServerError_verifyThrowException() throws Exception { HttpStatusException httpStatusException = mock(HttpStatusException.class); when(httpStatusException.wasServerError()).thenReturn(true); when(buttonApi.postOrder(any(Order.class), anyString(), anyString(), (String) isNull())).thenThrow(httpStatusException); task.execute(); }
@Test(expected = HttpStatusException.class) public void execute_httpStatusException_verifyThrowException() throws Exception { HttpStatusException httpStatusException = mock(HttpStatusException.class); when(buttonApi.postOrder(any(Order.class), anyString(), anyString(), (String) isNull())).thenThrow(httpStatusException); task.execute(); }