android登录凭证内容,android junit取决于登录凭证

我运行Android Studio与Android-Junit4的单元测试。 某些api请求需要依赖登录状态(Clerk)。 我试图从setup方法获得状态,但失败。android junit取决于登录凭证

@RunWith (UnitTestGradleTestRunner.class)

@Config (constants = BuildConfig.class)

public abstract class ApiTest {

protected HashMap 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 entry : map.entrySet()) {

final HBApiBaseTask requestTest = entry.getKey();

final ApiResponseTest responseTest = entry.getValue();

assertNotNull (requestTest);

assertNotNull (responseTest);

ApiTaskDelegate.getInstance().run (requestTest, new TaskHost() {

@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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值