I don't know how should I test this without really taking connection to real url to server.
I have read few articles about using Mockito in this kind of situation and tried to search around, but can not find good tutorial or advices how should I make jUnit-test for URL and URLConnection in my project.
Here is the code that I have problems when trying to test it:
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;
}
Here is also used imports I use for these, if someone wants to know:
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;
EDITED
Thanks for you answers, but it seems that I'm totally lost with this. Maybe I'm trying to think too complicated, but unit testing is pretty new stuff for me, but really want to learn it more.
Yes, I try to test getJSONObj-method, but those URL & URLConnection is making it difficult for me to understand how to test my method by "faking" it to believe it really takes connection.
Can't realize what you really mean, so here is the current code when I tried to do as you said Jeff Bowman. (Still using that big String, because I tried to get it first done with the current style and then get better performance with Reader's after this is working.)
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;
}
This test is almost useless to show, because I think it's totally wrong how I try to use the mock. (It's returning null when discusser.getJSONObj())
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;
}
}
Could you or someone else give guidance / example how getJSONObj()-method in Discusser should be tested?