URLConnection mockURLConnection = mock(HttpsURLConnection.class); URL mockUrl = mock(URL.class); when(new URL(anyString())).thenReturn(mockUrl); when(mockUrl.openConnection()).thenReturn(mockURLConnection);