From 44892b3eb0c6089f0cda91bacc4d55afe312207b Mon Sep 17 00:00:00 2001 From: Jacksgong Date: Sun, 18 Jun 2017 15:53:56 +0800 Subject: [PATCH] tests(DownloadRunnable): add unit test to testing the case of response code is not 200 or 206 --- .../download/DownloadRunnableTest.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/library/src/test/java/com/liulishuo/filedownloader/download/DownloadRunnableTest.java b/library/src/test/java/com/liulishuo/filedownloader/download/DownloadRunnableTest.java index d90e0919..518baeb9 100644 --- a/library/src/test/java/com/liulishuo/filedownloader/download/DownloadRunnableTest.java +++ b/library/src/test/java/com/liulishuo/filedownloader/download/DownloadRunnableTest.java @@ -16,6 +16,8 @@ package com.liulishuo.filedownloader.download; +import com.liulishuo.filedownloader.connection.FileDownloadConnection; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -23,12 +25,12 @@ 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; @@ -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)); }