BAEL-5776 - Mock a URL Connection in Java (#14184)
* BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson * BAEL-4756 - Mockito MockSettings * BAEL-4756 - Mockito MockSettings - fix spelling * BAEL-2674 - Upgrade the Okhttp article * BAEL-4204 - Adding Interceptors in OkHTTP * BAEL-4836 - Mocking Static Methods with Mockito * BAEL-4205 - A Guide to Events in OkHTTP * BAEL-5408 - Update Camel version in spring-boot-camel module * BAEL-5234 - Apache Camel Routes Testing in Spring Boot * BAEL-5234 - Apache Camel Routes Testing in Spring Boot * BAEL-5237 - Apache Camel Conditional Routing * BAEL-5236 - Apache Camel Exception Handling * BAEL-5890 - Jackson's Deserialization with Lombok * BAEL-5890 - Jackson's Deserialization with Lombok * BAEL-5890 - Jackson's Deserialization with Lombok * BAEL-5890 - Jackson's Deserialization with Lombok * BAEL-5890 - Jackson's Deserialization with Lombok * BAEL-5776 - Mock a URL Connection in Java * BAEL-5776 - Mock a URL Connection in Java --------- Co-authored-by: Jonathan Cook <jcook@sciops.esa.int> Co-authored-by: jcook02 <jonathan.paul.cook@ext.esa.int>
This commit is contained in:
		
							parent
							
								
									0d8db7af6a
								
							
						
					
					
						commit
						a0d7405115
					
				| @ -82,7 +82,31 @@ | ||||
|             <artifactId>converter-gson</artifactId> | ||||
|             <version>${retrofit.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.mockito</groupId> | ||||
|             <artifactId>mockito-inline</artifactId> | ||||
|             <version>${mockito.version}</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|         <dependency>  | ||||
|     		<groupId>org.jmockit</groupId>  | ||||
|     		<artifactId>jmockit</artifactId>  | ||||
|     		<version>${jmockit.version}</version> | ||||
| 		</dependency> | ||||
|     </dependencies> | ||||
|      | ||||
|     <build> | ||||
|     	<plugins> | ||||
|     		<plugin> | ||||
|             	<artifactId>maven-surefire-plugin</artifactId> | ||||
| 		            <configuration> | ||||
| 		               <argLine> | ||||
| 		                  -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar | ||||
| 		               </argLine> | ||||
| 		            </configuration> | ||||
| 	         </plugin> | ||||
|     	</plugins> | ||||
|     </build>      | ||||
| 
 | ||||
|     <properties> | ||||
|         <okhttp.version>4.9.1</okhttp.version> | ||||
| @ -95,6 +119,7 @@ | ||||
|         <spring.webflux.version>5.1.9.RELEASE</spring.webflux.version> | ||||
|         <reactive.stream.version>1.0.3</reactive.stream.version> | ||||
|         <reactor.version>3.2.12.RELEASE</reactor.version> | ||||
|         <jmockit.version>1.49</jmockit.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -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(); | ||||
|     } | ||||
| } | ||||
| @ -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 { | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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: "); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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: "); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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: "); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user