【发布时间】:2014-02-11 21:56:24
【问题描述】:
我刚刚意识到我有一些代码可以运行并且所有测试都通过了,尽管我从未指定要使用 List 接口的哪个具体实现。
这是有问题的函数
public List<Product> getProductList(int CategoryId)
{
List<Product> returnedProducts = null;
HttpGet httpGet = new HttpGet("https://www.shirts.io/api/v1/products/category/"+CategoryId+"/?api_key="+this.apiKey);
try
{
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200)
{
entity = response.getEntity();
jsonResponse = EntityUtils.toString(entity);
Type listType = new TypeToken<List<Product>>(){}.getType();
JsonNode root = mapper.readTree(jsonResponse);
ArrayNode products = (ArrayNode) root.path("result");
returnedProducts = (List<Product>) gson.fromJson(products.toString(), listType);
}
}
catch (IOException e)
{
e.printStackTrace();
}
return returnedProducts;
}
这是对上述功能的测试
public void testGetProductCategories()
{
List<Category> categories = null;
categories = productCalls.getProductCategories();
assertEquals(20,categories.size());
}
一切正常,没有给我任何编译错误。为什么会这样?我认为您必须指定您使用的是 ArrayList 还是 LinkedList。为什么我的代码无需我指定其中一个即可运行?
【问题讨论】:
标签: java arraylist linked-list