[英]How to mock HttpUrlConnection
I am trying to do a mock test for my class,but i am getting an error我正在尝试对我的 class 进行模拟测试,但出现错误
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:并测试 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); when(ConnectService.connectionGET(link)).thenReturn(mockConn);
to至
doReturn(mockConn).when(ConnectService).connectionGET(link); 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 setDoOutput(true)与 POST 一起使用以允许通过连接发送正文
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如果添加它, function 将直接转换为POST ,因此不会调用它,因为 API 支持GET而不是POST
Make a false or delete it造假或删除
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.