gpt4 book ai didi

java - 单元测试 HttpURL 连接

转载 作者:行者123 更新时间:2023-12-02 02:51:02 25 4
gpt4 key购买 nike

我正在为使用 java 中的 HttpUrlConnection 发出 Http POST 请求的方法编写单元测试。我正在使用 PowerMockito 来模拟 URL、代理。

@Before
public void setUp()
{
impl=new httpClient();

}
@Test
public void httpClientTest()
{
try {
URL obj= PowerMockito.mock(URL.class);
Proxy proxy = PowerMockito.mock(Proxy.class);
InetSocketAddress inetAddr=PowerMockito.mock(InetSocketAddress.class);
HttpURLConnection mockConn = PowerMockito.mock(HttpURLConnection.class);
PowerMockito.whenNew(InetSocketAddress.class).withArguments(anyString(),anyInt()).thenReturn(inetAddr);
PowerMockito.whenNew(URL.class).withArguments(any(String.class)).thenReturn(obj);
PowerMockito.whenNew(Proxy.class).withArguments(Proxy.Type.HTTP,inetAddr).thenReturn(proxy);
PowerMockito.when(obj.openConnection(proxy)).thenReturn(mockConn);
JsonObject req=new JsonObject();
String response=impl.makeRequest("abc",req);
System.out.println(response);
}
catch (Exception e)
{
System.out.println(e);
}
}



public void makeRequest(String empid,JsonObject request)
{
JsonObject variables=request.getJsonObject("variables");
variables.put("requested_for",empid);
request.put("variables",variables);
StringBuffer response = new StringBuffer();
JsonObject successResponse;
try
{
String url = "Some_valid_url";
URL obj = new URL(url);
String proxyURL="abc.xyz.com";
int port=80;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyURL, port));
HttpURLConnection con = (HttpURLConnection)obj.openConnection(proxy);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/json");
System.setProperty("com.sun.net.ssl.checkRevocation", "false"); //SSL certificate validation false
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
String requestBody= request.toString();
wr.writeBytes(requestBody);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
} catch (Exception e) {
System.out.println(e);
}


}

我在运行测试时遇到此异常。

  java.lang.IllegalArgumentException: type null is not compatible with address null 
at java.net.Proxy.<init>(Proxy.java:95)
at sun.net.ApplicationProxy.<init>(ApplicationProxy.java:37)
at sun.net.ApplicationProxy.create(ApplicationProxy.java:41)
at java.net.URL.openConnection(URL.java:1018)

异常被捕获 PowerMockito.when(obj.openConnection(proxy)).thenReturn(mockConn);测试中的这一点。

最佳答案

发现您的代码缺少两个注释和两个“when”。通过添加两个注释 @RunWith(PowerMockRunner.class)@PrepareForTest({URL.class}),您注意到的问题已得到解决。这样,final URL.class 就按预期被模拟了。

@RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
@PrepareForTest({java.net.URL.class})
public class UrlConnectionTest {

private HttpClient impl;

@Before
public void setUp() {
impl = new HttpClient();
}

@Test
public void httpClientTest() {
try {
URL obj = PowerMockito.mock(URL.class);
Proxy proxy = PowerMockito.mock(Proxy.class);
InetSocketAddress inetAddr = PowerMockito.mock(InetSocketAddress.class);
HttpURLConnection mockConn = PowerMockito.mock(HttpURLConnection.class);
PowerMockito.whenNew(InetSocketAddress.class).withArguments(anyString(),anyInt()).thenReturn(inetAddr);
PowerMockito.whenNew(URL.class).withArguments(any(String.class)).thenReturn(obj);
PowerMockito.whenNew(Proxy.class).withArguments(Proxy.Type.HTTP,inetAddr).thenReturn(proxy);
PowerMockito.when(obj.openConnection(proxy)).thenReturn(mockConn);
PowerMockito.when(mockConn.getOutputStream()).thenReturn(System.out);
InputStream myInputStream = new ByteArrayInputStream("Hello world".getBytes());
PowerMockito.when(mockConn.getInputStream()).thenReturn(myInputStream);
String response = impl.makeRequest("abc");
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
}

关于java - 单元测试 HttpURL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57108733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com