0
我试图测试我的类CachedGetRequest它从GoogleHttpClientSpiceRequest扩展。我如何测试Robospice GoogleHttpClientSpiceRequest?目前为HttpRequestFactory获取NullPointerException
继here我写了这个测试的例子:
@LargeTest
public class CachedGetRequestTest extends InstrumentationTestCase {
private MockWebServer mServer;
private CachedGetRequest<Product> mCachedGetRequest;
@Override
protected void setUp() throws Exception {
super.setUp();
mServer = new MockWebServer();
mServer.enqueue(new MockResponse().setBody(...));
mServer.start();
URL url = mServer.getUrl("/api/products/");
mCachedGetRequest = new CachedGetRequest<>(Product.class, url.toString(), "", "");
}
public void testLoadDataFromNetwork() throws Exception {
Product product = mCachedGetRequest.loadDataFromNetwork();
assertTrue(product.getName().equals("Test"));
}
}
但我得到一个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.api.client.http.HttpRequest com.google.api.client.http.HttpRequestFactory.buildRequest(java.lang.String, com.google.api.client.http.GenericUrl, com.google.api.client.http.HttpContent)' on a null object reference
如果我尝试使用像在使用的SimpleTextRequest一个简单的请求例如,那么它工作得很好。所以它适用于基本的SpiceRequest,但不适用于GoogleHttpClientSpiceRequest。
我看到HttpRequestFactory是空的,但我怎么能设置它?我可以在请求中调用setHttpRequestFactory,但我不能用Mockito嘲笑HttpRequestFactory,因为它是最终的类。我如何正确测试此请求?
谢谢!出于某种原因,我认为我不得不嘲笑HttpRequestFactory,但您的示例完美无缺。 – Kage