【发布时间】:2015-10-09 07:56:28
【问题描述】:
如果没有真正连接到服务器的真实网址,我不知道应该如何测试它。
我已经阅读了几篇关于在这种情况下使用 Mockito 的文章并尝试四处搜索,但找不到好的教程或建议我应该如何在我的项目中为 URL 和 URLConnection 进行 jUnit-test。
这是我在尝试测试时遇到问题的代码:
public JSONObject getJSONObj()
throws MalformedURLException, IOException, ParseException {
String jsonString;
try (InputStream is = getURLConnection("RealUrlStringGoesHere").getInputStream();) {
jsonString = IOUtils.toString(is);
}
return (JSONObject) JSONValue.parseWithException(jsonString);
}
public URLConnection getURLConnection(String urlStr) throws MalformedURLException, IOException {
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
return conn;
}
如果有人想知道,这里也使用了我用于这些的导入:
import java.net.URLConnection;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
已编辑
感谢您的回答,但似乎我对此完全迷失了。也许我想的太复杂了,但是单元测试对我来说是很新的东西,但我真的很想学习更多。
是的,我尝试测试 getJSONObj-method,但是那些 URL 和 URLConnection 让我很难理解如何通过“假装”它来测试我的方法,以相信它确实需要连接。
无法理解您的真正意思,所以这是我尝试按照您所说的 Jeff Bowman 进行操作时的当前代码。 (仍然使用那个大字符串,因为我尝试先用当前样式完成它,然后在它工作后使用 Reader 获得更好的性能。)
Discusser.java
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
public class Discusser implements URLOpener {
public JSONObject getJSONObj() throws IOException, ParseException {
String jsonString;
try (InputStream is = openURL("RealUrlStringGoesHere");) {
jsonString = IOUtils.toString(is);
}
return (JSONObject) JSONValue.parseWithException(jsonString);
}
@Override
public InputStream openURL(String urlStr) throws IOException {
URL url = new URL(urlStr);
URLConnection urlConnection = url.openConnection();
return urlConnection.getInputStream();
}
}
URLOpener.java
import java.io.IOException;
import java.io.InputStream;
public interface URLOpener {
InputStream openURL(String urlStr) throws IOException;
}
这个测试几乎没有用,因为我认为我如何尝试使用模拟是完全错误的。 (当讨论者.getJSONObj()返回null)
DiscusserTest.java
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import org.json.simple.JSONObject;
import org.junit.Test;
import org.mockito.Mockito;
public class DiscusserTest {
@Test
public void testGetJSONObj() throws Exception {
JSONObject expectedJSONObject = createExpected();
ByteArrayInputStream inputForMock = new ByteArrayInputStream(generateJSONString().getBytes("UTF-8"));
// Should I mock like this or...
Discusser discusser = Mockito.mock(Discusser.class);
Mockito.when(discusser.openURL("asd")).thenReturn(inputForMock);
//
assertEquals(expectedJSONObject, discusser.getJSONObj());
}
private String generateJSONString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append("\"id\":\"123\",");
sb.append("\"name\":\"test\"");
sb.append("}");
return sb.toString();
}
@SuppressWarnings("unchecked")
private JSONObject createExpected() {
JSONObject obj = new JSONObject();
obj.put("id", 123);
obj.put("name", "test");
return obj;
}
}
您或其他人能否提供指导/示例应如何测试 Discusser 中的 getJSONObj()-method?
【问题讨论】:
标签: java url junit mockito urlconnection