我有一些API的實現。PowerMockito可以檢查whenNew的參數嗎?
例如:
public class SoPostApi implements SoApi {
...
@Override
public void searchOrganizations(String query, ...) {
String jsonResponse = performPostMethod("SearchOrg", ...);
...
}
@Override
public void searchCategories(String query, ...) {
String jsonResponse = performPostMethod("SearchCat", ...);
...
}
private String performPostMethod(String methodName, ...) {
URL url = new URL(API_URL + methodName);
...
}
而且我想用PowerMockito進行測試。
我怎麼看它:的URL.class
- 捕捉新的實例,並返回
URL.class
說法mocked URL
- 檢查新實例包含子取決於
- 模擬
openConnection()
方法(methodName.contains("SearchOrg")
,但怎麼樣?)methodName
例如:
如果方法performPostMethod實例化一個新的
URL.class
與String
參數包含"SearchOrg"
那麼我想URL
取代它在其中方法openConnection()
返回HttpURLConnection
可配置該參數(getInputStream()
必須與組織的名單返回JSON)。如果方法performPostMethod實例化一個新的
URL.class
與String
參數包含"SearchCat"
那麼我想URL
取代它在其中方法openConnection()
返回HttpURLConnection
可配置該參數(getInputStream()
必須類別列表返回JSON)。
是否可以實現?
嗨@Alexandr,你能檢查我的答案嗎?它有助於你解決問題嗎?如果您有任何疑問,請告訴我。 – troig
@謝謝你,謝謝。我很忙,我儘快檢查它:) – Alexandr
完全不用擔心 – troig