public abstract class A {
public String method1( String urlString ) {
HTTP HttpURLConnection con = getConnection(url);
.... I use con.getInpurStream() to get the data and return it...
}
public HttpURLConnection getConnection(URL url) {
URL url = new URL(urlString);
return (HttpURLConnection) url.openConnection();
}
}
public class B extends A {
public String method2(String urlString) {
return method1(urlString);
}
}
Im trying to write unit test for Class B method2 using JUNIT and Mockito.
I tired something like this :
public class testB {
String urlString = "someValid.url";
String dummyRes = "dummy Response";
InputStream res = new ByteArrayInputStream(dummyRes.getBytes());
B mockB = Mockito.mock(B.class);
HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
Mockito.when(connection.getInputStream()).thenReturn(res);
Mockito.when(mockB.getConnection(urlString)).thenRetrun(connection);
assertEquals("dummy Response", mockB.method2("someValid.url");
}
I keep getting either the real time data from that url instead of dummyRes.
Please help /\
Edit1:
Removed the static methods. And im still not able to mock the getConnection() method to return the mocked connection.
Edit2:
Moved making URL object to getConnection method to avoid making a new URL so that I can mock it and made changes in the test for that. Still picking up real time values.