Skip to content

Commit

Permalink
tests(DownloadRunnable): add unit test to testing the case of respons…
Browse files Browse the repository at this point in the history
…e code is not 200 or 206
  • Loading branch information
Jacksgong committed Jun 18, 2017
1 parent b74b2b4 commit 44892b3
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@

package com.liulishuo.filedownloader.download;

import com.liulishuo.filedownloader.connection.FileDownloadConnection;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.robolectric.RobolectricTestRunner;

import java.io.IOException;
import java.net.HttpURLConnection;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
Expand Down Expand Up @@ -69,13 +71,17 @@ public void run_withConnectFailed_retry() throws IOException {
}

@Test
public void run_withConnectButCreateFetchTaskFailed_error() {
// create fetch-data-task would be crash because of arguments not ready
public void run_responseCodeNotMet_error() throws IOException {
final FileDownloadConnection connection = mock(FileDownloadConnection.class);
when(connection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_PRECON_FAILED);
when(mockConnectTask.connect()).thenReturn(connection);

downloadRunnable.run();

verify(mockCallback, times(0)).onRetry(any(Exception.class), anyLong());
// retry first.
verify(mockCallback).onRetry(any(Exception.class), anyLong());

// then callback error.
verify(mockCallback).onError(any(Exception.class));
}

Expand Down

0 comments on commit 44892b3

Please sign in to comment.