diff --git a/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileDownloaderIntegrationTest.java b/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileDownloaderIntegrationTest.java index 4fa6c0a466..6f0f705350 100644 --- a/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileDownloaderIntegrationTest.java +++ b/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileDownloaderIntegrationTest.java @@ -8,10 +8,10 @@ import org.junit.Test; import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class BinaryFileDownloaderIntegrationTest { @@ -19,19 +19,21 @@ public class BinaryFileDownloaderIntegrationTest { public MockWebServer server = new MockWebServer(); @Test - public void givenATextFile_whenDownload_thenExpectFileDownloaded() throws IOException { + public void givenATextFile_whenDownload_thenExpectFileDownloaded() { String body = "Hello Baeldung Readers!"; server.enqueue(new MockResponse().setBody(body)); String fileName = "download.txt"; - BinaryFileWriter writer = new BinaryFileWriter(new FileOutputStream(fileName), progress -> assertEquals(100.0, progress, .0)); - BinaryFileDownloader tested = new BinaryFileDownloader(new OkHttpClient(), writer); - long downloaded = tested.download(server.url("/greetings").toString()); - - assertEquals(body.length(), downloaded); - File downloadedFile = new File(fileName); - assertTrue(downloadedFile.isFile()); - assertTrue(downloadedFile.delete()); + ProgressCallback progressCallback = progress -> assertEquals(100.0, progress, .0); + try (BinaryFileWriter writer = new BinaryFileWriter(new FileOutputStream(fileName), progressCallback); BinaryFileDownloader tested = new BinaryFileDownloader(new OkHttpClient(), writer)) { + long downloaded = tested.download(server.url("/greetings").toString()); + assertEquals(body.length(), downloaded); + File downloadedFile = new File(fileName); + assertTrue(downloadedFile.isFile()); + assertTrue(downloadedFile.delete()); + } catch (Exception e) { + fail("An unexpected exception has occurred: " + e); + } } } diff --git a/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileDownloaderUnitTest.java b/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileDownloaderUnitTest.java index 20240febb3..15dda3a471 100644 --- a/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileDownloaderUnitTest.java +++ b/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileDownloaderUnitTest.java @@ -16,8 +16,7 @@ import org.mockito.junit.MockitoJUnitRunner; import java.io.InputStream; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.Mockito.mock; @@ -53,7 +52,7 @@ public class BinaryFileDownloaderUnitTest { verify(writer).close(); } - @Test + @Test(expected = IllegalStateException.class) public void givenUrlAndResponseWithNullBody_whenDownload_thenExpectIllegalStateException() throws Exception { String url = "http://example.com/file"; Call call = mock(Call.class); @@ -61,7 +60,7 @@ public class BinaryFileDownloaderUnitTest { Response response = createResponse(url, null); when(call.execute()).thenReturn(response); - assertThrows(IllegalStateException.class, () -> tested.download(url)); + tested.download(url); verify(writer, times(0)).write(any(InputStream.class), anyDouble()); } diff --git a/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileWriterUnitTest.java b/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileWriterUnitTest.java index 19e3d321c2..2b3f0a1313 100644 --- a/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileWriterUnitTest.java +++ b/libraries-http-2/src/test/java/com/baeldung/okhttp/download/BinaryFileWriterUnitTest.java @@ -8,7 +8,7 @@ import org.mockito.junit.MockitoJUnitRunner; import java.io.InputStream; import java.io.OutputStream; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; @@ -28,7 +28,7 @@ public class BinaryFileWriterUnitTest { InputStream inputStream = mock(InputStream.class); when(inputStream.read(any(), anyInt(), anyInt())).thenReturn(10, -1); - try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress))) { + try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress, .0))) { long result = tested.write(inputStream, 10); assertEquals(10, result); @@ -42,7 +42,7 @@ public class BinaryFileWriterUnitTest { public void givenInputStreamEmpty_whenWrite_thenExpectNotWritten() throws Exception { InputStream inputStream = mock(InputStream.class); - try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress))) { + try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress, .0))) { long result = tested.write(inputStream, 1); assertEquals(0, result);