简体   繁体   中英

How to mock HttpUrlConnection

I am trying to do a mock test for my class,but i am getting an error

Code:

public final class ConnectService {
  public static HttpURLConnection connectionGET(String urlToValidate) throws IOException {
    HttpURLConnection huc = null;
    int responseCode;
    boolean response;

    try {
      URL url = new URL(urlToValidate);
      huc = (HttpURLConnection) url.openConnection();
      huc.setDoOutput(true);
      huc.setRequestMethod("GET");
      huc.setUseCaches(false);
      huc.setConnectTimeout(50000);
      huc.setReadTimeout(50000);
      huc.setRequestProperty("Content-Type", "application/json");
      huc.connect();
    } finally {
      if (huc != null) {
        huc.disconnect();
      }
    }
    return huc;
  }

and Test class:

@Test
   public void checkConnectionGET_withMocking() throws Exception {
      URL url = PowerMockito.mock(URL.class);
      HttpURLConnection mockConn = Mockito.mock(HttpURLConnection.class);
      System.out.println(mockConn);
      String link = "http://url.com";

      when(ConnectService.connectionGET(link)).thenReturn(mockConn);
   }

Error:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
HttpURLConnection$$EnhancerByMockitoWithCGLIB$$a87fadd4 cannot be returned by toString()
toString() should return String
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

    at ConnectServiceTest.checkConnectionGET_withMocking(ConnectServiceTest.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

I Modified

when(ConnectService.connectionGET(link)).thenReturn(mockConn);

to

doReturn(mockConn).when(ConnectService).connectionGET(link);

But then i get the below error:

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to when() is not a mock!
Example of correct stubbing:
    doThrow(new RuntimeException()).when(mock).someMethod();
    at ConnectServiceTest.checkConnectionGET_withMocking(ConnectServiceTest.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

Could you please help with this

setDoOutput(true) is used with POST to allow sending a body via the connection

If you add it, the function will be converted directly to POST , so it will not be called because the API supports GET and not POST

Make a false or delete it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM