diff --git a/libraries-http-2/pom.xml b/libraries-http-2/pom.xml
index d8479def3c..77498dd248 100644
--- a/libraries-http-2/pom.xml
+++ b/libraries-http-2/pom.xml
@@ -82,7 +82,31 @@
converter-gson
${retrofit.version}
+
+ org.mockito
+ mockito-inline
+ ${mockito.version}
+ test
+
+
+ org.jmockit
+ jmockit
+ ${jmockit.version}
+
+
+
+
+
+ maven-surefire-plugin
+
+
+ -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
+
+
+
+
+
4.9.1
@@ -95,6 +119,7 @@
5.1.9.RELEASE
1.0.3
3.2.12.RELEASE
+ 1.49
\ No newline at end of file
diff --git a/libraries-http-2/src/main/java/com/baeldung/mock/url/UrlFetcher.java b/libraries-http-2/src/main/java/com/baeldung/mock/url/UrlFetcher.java
new file mode 100644
index 0000000000..bb9ba4a5f9
--- /dev/null
+++ b/libraries-http-2/src/main/java/com/baeldung/mock/url/UrlFetcher.java
@@ -0,0 +1,23 @@
+package com.baeldung.mock.url;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class UrlFetcher {
+
+ private URL url;
+
+ public UrlFetcher(URL url) throws IOException {
+ this.url = url;
+ }
+
+ public boolean isUrlAvailable() throws IOException {
+ return getResponseCode() == HttpURLConnection.HTTP_OK;
+ }
+
+ private int getResponseCode() throws IOException {
+ HttpURLConnection con = (HttpURLConnection) this.url.openConnection();
+ return con.getResponseCode();
+ }
+}
diff --git a/libraries-http-2/src/test/java/com/baeldung/mock/url/MockHttpURLConnection.java b/libraries-http-2/src/test/java/com/baeldung/mock/url/MockHttpURLConnection.java
new file mode 100644
index 0000000000..9df05fe0d0
--- /dev/null
+++ b/libraries-http-2/src/test/java/com/baeldung/mock/url/MockHttpURLConnection.java
@@ -0,0 +1,35 @@
+package com.baeldung.mock.url;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class MockHttpURLConnection extends HttpURLConnection {
+
+ protected MockHttpURLConnection(URL url) {
+ super(url);
+ }
+
+ @Override
+ public int getResponseCode() {
+ return responseCode;
+ }
+
+ public void setResponseCode(int responseCode) {
+ this.responseCode = responseCode;
+ }
+
+ @Override
+ public void disconnect() {
+ }
+
+ @Override
+ public boolean usingProxy() {
+ return false;
+ }
+
+ @Override
+ public void connect() throws IOException {
+ }
+
+}
diff --git a/libraries-http-2/src/test/java/com/baeldung/mock/url/MockURLStreamHandler.java b/libraries-http-2/src/test/java/com/baeldung/mock/url/MockURLStreamHandler.java
new file mode 100644
index 0000000000..1cc09dc2e9
--- /dev/null
+++ b/libraries-http-2/src/test/java/com/baeldung/mock/url/MockURLStreamHandler.java
@@ -0,0 +1,21 @@
+package com.baeldung.mock.url;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+public class MockURLStreamHandler extends URLStreamHandler {
+
+ private MockHttpURLConnection mockHttpURLConnection;
+
+ public MockURLStreamHandler(MockHttpURLConnection mockHttpURLConnection) {
+ this.mockHttpURLConnection = mockHttpURLConnection;
+ }
+
+ @Override
+ protected URLConnection openConnection(URL url) throws IOException {
+ return this.mockHttpURLConnection;
+ }
+
+}
diff --git a/libraries-http-2/src/test/java/com/baeldung/mock/url/MockURLStreamHandlerFactory.java b/libraries-http-2/src/test/java/com/baeldung/mock/url/MockURLStreamHandlerFactory.java
new file mode 100644
index 0000000000..855c761b65
--- /dev/null
+++ b/libraries-http-2/src/test/java/com/baeldung/mock/url/MockURLStreamHandlerFactory.java
@@ -0,0 +1,19 @@
+package com.baeldung.mock.url;
+
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
+
+public class MockURLStreamHandlerFactory implements URLStreamHandlerFactory {
+
+ private MockHttpURLConnection mockHttpURLConnection;
+
+ public MockURLStreamHandlerFactory(MockHttpURLConnection mockHttpURLConnection) {
+ this.mockHttpURLConnection = mockHttpURLConnection;
+ }
+
+ @Override
+ public URLStreamHandler createURLStreamHandler(String protocol) {
+ return new MockURLStreamHandler(this.mockHttpURLConnection);
+ }
+
+}
diff --git a/libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherJMockitUnitTest.java b/libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherJMockitUnitTest.java
new file mode 100644
index 0000000000..b99dcd282d
--- /dev/null
+++ b/libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherJMockitUnitTest.java
@@ -0,0 +1,42 @@
+package com.baeldung.mock.url;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import mockit.Expectations;
+import mockit.Mocked;
+import mockit.integration.junit5.JMockitExtension;
+
+
+@ExtendWith(JMockitExtension.class)
+class UrlFetcherJMockitUnitTest {
+
+ @Test
+ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableTrue(@Mocked URL anyURL, @Mocked HttpURLConnection mockConn) throws Exception {
+ new Expectations() {{
+ mockConn.getResponseCode();
+ result = HttpURLConnection.HTTP_OK;
+ }};
+
+ UrlFetcher fetcher = new UrlFetcher(new URL("https://www.baeldung.com/"));
+ assertTrue(fetcher.isUrlAvailable(), "Url should be available: ");
+ }
+
+ @Test
+ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableFalse(@Mocked URL anyURL, @Mocked HttpURLConnection mockConn) throws Exception {
+ new Expectations() {{
+ mockConn.getResponseCode();
+ result = HttpURLConnection.HTTP_INTERNAL_ERROR;
+ }};
+
+ UrlFetcher fetcher = new UrlFetcher(new URL("https://www.baeldung.com/"));
+ assertFalse(fetcher.isUrlAvailable(), "Url should NOT be available: ");
+ }
+
+}
diff --git a/libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherMockitoUnitTest.java b/libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherMockitoUnitTest.java
new file mode 100644
index 0000000000..bd998e83b4
--- /dev/null
+++ b/libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherMockitoUnitTest.java
@@ -0,0 +1,39 @@
+package com.baeldung.mock.url;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.junit.jupiter.api.Test;
+
+class UrlFetcherMockitoUnitTest {
+
+ @Test
+ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableTrue() throws Exception {
+ HttpURLConnection mockHttpURLConnection = mock(HttpURLConnection.class);
+ when(mockHttpURLConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
+
+ URL mockURL = mock(URL.class);
+ when(mockURL.openConnection()).thenReturn(mockHttpURLConnection);
+
+ UrlFetcher fetcher = new UrlFetcher(mockURL);
+ assertTrue(fetcher.isUrlAvailable(), "Url should be available: ");
+ }
+
+ @Test
+ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableFalse() throws Exception {
+ HttpURLConnection mockHttpURLConnection = mock(HttpURLConnection.class);
+ when(mockHttpURLConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_NOT_FOUND);
+
+ URL mockURL = mock(URL.class);
+ when(mockURL.openConnection()).thenReturn(mockHttpURLConnection);
+
+ UrlFetcher fetcher = new UrlFetcher(mockURL);
+ assertFalse(fetcher.isUrlAvailable(), "Url should NOT be available: ");
+ }
+
+}
diff --git a/libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherUnitTest.java b/libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherUnitTest.java
new file mode 100644
index 0000000000..be3a784a99
--- /dev/null
+++ b/libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherUnitTest.java
@@ -0,0 +1,40 @@
+package com.baeldung.mock.url;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+class UrlFetcherUnitTest {
+
+ private static MockHttpURLConnection mockHttpURLConnection;
+
+ @BeforeAll
+ public static void setUp() {
+ mockHttpURLConnection = new MockHttpURLConnection(null);
+ URL.setURLStreamHandlerFactory(new MockURLStreamHandlerFactory(mockHttpURLConnection));
+ }
+
+ @Test
+ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableTrue() throws Exception {
+ mockHttpURLConnection.setResponseCode(HttpURLConnection.HTTP_OK);
+ URL url = new URL("https://www.baeldung.com/");
+
+ UrlFetcher fetcher = new UrlFetcher(url);
+ assertTrue(fetcher.isUrlAvailable(), "Url should be available: ");
+ }
+
+ @Test
+ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableFalse() throws Exception {
+ mockHttpURLConnection.setResponseCode(HttpURLConnection.HTTP_FORBIDDEN);
+ URL url = new URL("https://www.baeldung.com/");
+
+ UrlFetcher fetcher = new UrlFetcher(url);
+ assertFalse(fetcher.isUrlAvailable(), "Url should NOT be available: ");
+ }
+
+}