0
我運行Android Studio
與Android-Junit4
的單元測試。 某些api請求需要依賴登錄狀態(Clerk
)。 我試圖從setup
方法獲得狀態,但失敗。android junit取決於登錄憑證
@RunWith (UnitTestGradleTestRunner.class)
@Config (constants = BuildConfig.class)
public abstract class ApiTest {
protected HashMap<HBApiBaseTask, ApiResponseTest> map = new HashMap<>();
protected synchronized void add (HBApiBaseTask request, ApiResponseTest response) {
map.put (request, response);
}
protected synchronized void remove (HBApiBaseTask request) {
map.remove (request);
}
protected synchronized void runApiNow() {
if (map.size() == 0) return;
for (Map.Entry<HBApiBaseTask, ApiResponseTest> entry : map.entrySet()) {
final HBApiBaseTask requestTest = entry.getKey();
final ApiResponseTest responseTest = entry.getValue();
assertNotNull (requestTest);
assertNotNull (responseTest);
ApiTaskDelegate.getInstance().run (requestTest, new TaskHost<InnerNetworkResponse>() {
@Override
public void onPostExecute (int requestCode, InnerNetworkResponse innerNetworkResponse) {
super.onPostExecute (requestCode, innerNetworkResponse);
assertNotNull (innerNetworkResponse);
assertNotNull (responseTest);
responseTest.onResponse (requestCode, innerNetworkResponse);
switch (innerNetworkResponse.getStatusCode()) {
case IStatusCode.CODE_SUCCESS:
JSONObject json = innerNetworkResponse.getResponse();
assertNotNull (json);
if (responseTest instanceof ApiResponseWithParserCheckTest) {
if (json != null) {
((ApiResponseWithParserCheckTest) responseTest).checkPOJO (innerNetworkResponse);
}
}
break;
default:
break;
}
}
}, true);
}
map.clear();
}
}
測試用例
@RunWith (UnitTestGradleTestRunner.class)
@Config (constants = BuildConfig.class)
public class SendCodeTaskTest extends ApiTest {
private Clerk clerk;
@Before
public void setup() {
clerk = loginAsync();
}
public Clerk loginAsync() {
final Clerk clerk = new Clerk();
final CountDownLatch latch = new CountDownLatch (1);
final Credential credential = new Credential();
credential.setAccount ("account");
credential.setPassword ("password");
add (new LoginTask (credential), new ApiResponseWithParserCheckTest() {
@Override
public void checkPOJO (InnerNetworkResponse response) {
assertEquals (response.getStatusCode(), IStatusCode.CODE_SUCCESS);
assertNotNull (response);
final JSONObject json = response.getResponse();
assertNotNull (json);
JSONObject model = JSONParseUtil.getModel (json);
assertNotNull (model);
credential.parse (model);
assertNotNull (clerk);
clerk.setCredential (credential);
}
@Override
public void onResponse (int requestCode, InnerNetworkResponse innerNetworkResponse) {
switch (innerNetworkResponse.getStatusCode()) {
case IStatusCode.CODE_SUCCESS:
assertTrue (true);
break;
default:
assertTrue (false);
}
latch.countDown();
}
});
runApiNow();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertNotNull (clerk);
return clerk;
}
}
錯誤日誌:
java.net.SocketTimeoutException: timeout
at okio.Okio$3.newTimeoutException(Okio.java:207)
at okio.AsyncTimeout.exit(AsyncTimeout.java:261)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:215)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906)
at com.squareup.okhttp.internal.http.HttpEngine.access$300(HttpEngine.java:92)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:891)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:749)
at com.squareup.okhttp.Call.getResponse(Call.java:268)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
at com.squareup.okhttp.Call.execute(Call.java:79)