我在为我的代码编写测试用例时出现以下异常。我嘲笑HttpURLConnection,但仍然在调试时,我可以看到它采用了我在测试中提供的虚拟URL的值。服务器返回的HTTP响应代码:400:当嘲笑HttpURLConnection
java.io.IOException: Server returned HTTP response code: 400 for URL: http://dummyURL.com
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at tcd.edu.repositoryextractor.PullObject.fetchJSONString(PullObject.java:23)
at tcd.edu.repositoryextractor.PullObjectTest.testPullObjectWithValidString(PullObjectTest.java:64)
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)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
以下是测试类
public class PullObjectTest {
@InjectMocks
PullObject pullObject = new PullObject();
@Mock
WrappedUrl mockedURL;
@Mock
HttpURLConnection mockedHttpConnection;
@Mock
BufferedReader mockedReader;
@Mock
InputStream mockedInputStream;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testPullObjectForNullString() {
assertNull(pullObject.fetchJSONString(null));
}
@SuppressWarnings("rawtypes")
@Test
public void testPullObjectWithValidString() {
try {
Mockito.when(mockedURL.openConnection()).thenReturn(mockedHttpConnection);
Mockito.when(mockedHttpConnection.getInputStream()).thenReturn(mockedInputStream);
Mockito.when(mockedReader.readLine()).thenAnswer(new Answer() {
private int count = 1;
public Object answer(InvocationOnMock invocation) throws Throwable {
if (count++ == 1)
return "This is String";
return null;
}
});
assertEquals("This is String", pullObject.fetchJSONString("http://dummyURL.com"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
以下是我需要测试
public class PullObject {
public String fetchJSONString(String url) {
if (url == null)
return null;
StringBuffer bufferedResponse = new StringBuffer();
try {
WrappedUrl obj = new WrappedUrl(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
bufferedResponse = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
bufferedResponse.append(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return bufferedResponse.toString();
}
}
class WrappedUrl {
URL obj;
WrappedUrl(String url) {
try {
obj = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public URLConnection openConnection() {
URLConnection connection = null;
try {
connection = obj.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
return connection;
}
}
好像你实际上得到HttpURLConnection类的真实实例。 HttpURLConnection con =(HttpURLConnection)obj.openConnection();您应该尝试依赖注入或将其设置为您在测试中创建的模拟对象。你是否尝试过一个断点并验证它是一个模拟? – isuPatches
我对Mockito有点新鲜。所以当我做Mockito.when(mockedURL.openConnection())。然后返回(mockedHttpConnection);它不会返回HttpURLConnection的模拟实例吗? –
我应用了断点,但它不模拟HttpURLConnection,我不知道为什么。 –