From f274a0d2032e07c9b1e152971c842d8e7e4a8f57 Mon Sep 17 00:00:00 2001 From: mia-li-001 Date: Sat, 31 Oct 2026 19:50:13 -0500 Subject: [PATCH] Extracted duplicated stubbings in HttpClientDefaultTest into 2 helpers --- .../internal/HttpClientDefaultTest.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/github/davidmoten/aws/lw/client/internal/HttpClientDefaultTest.java b/src/test/java/com/github/davidmoten/aws/lw/client/internal/HttpClientDefaultTest.java index 35c86341..d1971de6 100644 --- a/src/test/java/com/github/davidmoten/aws/lw/client/internal/HttpClientDefaultTest.java +++ b/src/test/java/com/github/davidmoten/aws/lw/client/internal/HttpClientDefaultTest.java @@ -21,8 +21,7 @@ public class HttpClientDefaultTest { public void testGetInputStreamThrows() throws IOException { HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); when(connection.getInputStream()).thenThrow(IOException.class); - when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream()); - when(connection.getResponseCode()).thenReturn(200); + setupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubs(connection, 200); try { HttpClientDefault.request(connection, new byte[0]); Assert.fail(); @@ -36,8 +35,7 @@ public void testGetInputStreamThrows() throws IOException { public void testDisconnectThrows() throws IOException { HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); when(connection.getInputStream()).thenThrow(IOException.class); - when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream()); - when(connection.getResponseCode()).thenReturn(200); + setupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubs(connection, 200); Mockito.doThrow(RuntimeException.class).when(connection).disconnect(); try { HttpClientDefault.request(connection, new byte[0]); @@ -51,9 +49,7 @@ public void testDisconnectThrows() throws IOException { @Test public void testGetInputStreamReturnsNull() throws IOException { HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); - when(connection.getInputStream()).thenReturn(null); - when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream()); - when(connection.getResponseCode()).thenReturn(200); + setupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs(connection, 200); try (ResponseInputStream response = HttpClientDefault.request(connection, new byte[0])) { assertEquals(200, response.statusCode()); assertEquals(-1, response.read()); @@ -64,9 +60,7 @@ public void testGetInputStreamReturnsNull() throws IOException { @Test public void testIsOKWhenNotOk() throws IOException { HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); - when(connection.getInputStream()).thenReturn(null); - when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream()); - when(connection.getResponseCode()).thenReturn(500); + setupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs(connection, 500); try (ResponseInputStream response = HttpClientDefault.request(connection, new byte[0])) { assertEquals(500, response.statusCode()); assertEquals(-1, response.read()); @@ -77,9 +71,7 @@ public void testIsOKWhenNotOk() throws IOException { @Test public void testIsOKWhenNotOkStatusCodeLessThan200() throws IOException { HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); - when(connection.getInputStream()).thenReturn(null); - when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream()); - when(connection.getResponseCode()).thenReturn(100); + setupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs(connection, 100); try (ResponseInputStream response = HttpClientDefault.request(connection, new byte[0])) { assertEquals(100, response.statusCode()); assertEquals(-1, response.read()); @@ -87,4 +79,13 @@ public void testIsOKWhenNotOkStatusCodeLessThan200() throws IOException { } } + private void setupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubs(HttpURLConnection connection, int getResponseCodeReturnValue) throws IOException { + when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream()); + when(connection.getResponseCode()).thenReturn(getResponseCodeReturnValue); + } + + private void setupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs(HttpURLConnection connection, int getResponseCodeReturnValue) throws IOException { + setupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubs(connection, getResponseCodeReturnValue); + when(connection.getInputStream()).thenReturn(null); + } }