I have a final class which tries to connect to a provided URL using openConnection()
. I mocked it to have it return an UnknownHostException
because I know that the provided URL is unknown and to also reduce the time of the unit test (> 0.01 sec is too long for this one test).
Class to be tested:
public final class Server {
private Server() {}
public static void getServerStuff(final URL url) throws IOException {
try {
final URLConn urlConn;
// immediately throw exception without trying to make a connection
urlConn = url.openConnection();
}
catch (Exception e) {
// not relevant
}
}
}
Unit test
public class ServerTest {
public class UrlWrapper {
Url url;
public UrlWrapper(String spec) throws MalformedURLException {
url = new URL(spec);
}
public URLConnection openConnection() throws IOException {
return url.openConnection();
}
}
public void testUnknownHostExceptionIsThrown() throws IOException {
UrlWrapper mockUrl = mock(UrlWrapper.class);
URLConnection mockUrlConn = mock(URLConnection.class);
when(mockUrl.openConnection()).thenThrow(UnknownHostException.class);
final String server = "blabla";
final URL url = new URL("http://" + server);
mockUrl.url = url;
try {
Server.getServerStuff(mockUrl.url);
}
catch (IOException e) {
// do stuff with Assert
}
}
}
I am required to use mockito instead of powermockito which can mock final classes. My main problem is that I do not know how to tell the unit test to use my mocked openConnection()
. It should still test my getServerStuff()
method but throw the exception without actually trying to connect.
What do I need to change to make it work?
EDIT: I do not think it is a duplicate of the referenced question because I know how to mock a final class (using a wrapper, e.g). My problem is the next step, meaning how to use my mocked method. My unit test will go into the to-be tested method and use the openConnection()
from standard lib but I want it to use my mocked one to reduce time needed for this unit test to complete.