This is the class I'm trying to test (it calculates the size of HTTP page):
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.*;
public class Loader {
private Client client;
public Loader(Client c) {
this.client = c;
}
public Integer getLength(URI uri) throws Exception {
return c.resource(uri) // returns WebResource
.accept(MediaType.APPLICATION_XML) // returns WebResource.Builder
.get(String.class) // returns String
.length();
}
}
Of course it just an example, not a real-life solution. Now I'm trying to test this class:
public class LoaderTest {
@Test public void shouldCalculateLength() throws Exception {
String mockPage = "test page"; // length is 9
Client mockedClient = /* ??? */;
Loader mockedLoader = new Loader(mockedClient);
assertEquals(
mockPage.length(),
mockedLoader.getLength(new URI("http://example.com"))
);
}
}
How should I mock com.sun.jersey.api.client.Client
class? I'm trying to use Mockito, but any other framework will be OK, since I'm a newbie here..