我正在为使用Java中的HttpUrlConnection发出Http POST请求的方法编写单元测试。我正在使用PowerMockito来模拟URL,Proxy。
@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}(,您注意到的问题已得到解决。这样,最终网址.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);
}
}
}