如何模拟Google API AndroidPublisher请求 [英] How to mock Google API AndroidPublisher request

查看:563
本文介绍了如何模拟Google API AndroidPublisher请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向AndroidPublisher API发出请求的类,以获取 Purchases.Subscriptions.get 资源.如何使用包com.google.api.client.testing中的类模拟出OAuth请求并调用以获取订阅资源?

I've got a class that makes a request to the AndroidPublisher API to get the Purchases.Subscriptions.get resource. How does one mock out the OAuth request and call to get the subscription resource using the classes from the package com.google.api.client.testing?

以下是获取订阅的代码:

Here is the code to get the subscription:

HttpTransport httpTransport = new NetHttpTransport.Builder().build();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(httpTransport)
    .setJsonFactory(jsonFactory)
    .setServiceAccountId("email")
    .setServiceAccountScopes(singleton(AndroidPublisherScopes.ANDROIDPUBLISHER))
    .setServiceAccountPrivateKeyFromP12File(new File("file.p12"))
    .build();
AndroidPublisher androidPublisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential)
    .setApplicationName("appname")
    .build();
AndroidPublisher.Purchases.Subscriptions.Get get = androidPublisher.purchases().subscriptions().get(
    "appname",
    "productId",
    "purchaseToken");
SubscriptionPurchase subscription = get.execute();
System.out.println(subscription.toPrettyString());

这是我目前的样子,但尝试模拟JSON响应却失败了:

Here is how I am currently, but unsuccessfully, trying to mock out the JSON responses:

MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
mockResponse.setStatusCode(200);
mockResponse.setContentType(Json.MEDIA_TYPE);
mockResponse.setContent("{\n" +
        "  \"autoRenewing\" : true,\n" +
        "  \"countryCode\" : \"US\",\n" +
        "  \"developerPayload\" : \"\",\n" +
        "  \"expiryTimeMillis\" : \"1480019001357\",\n" +
        "  \"kind\" : \"androidpublisher#subscriptionPurchase\",\n" +
        "  \"paymentState\" : 1,\n" +
        "  \"priceAmountMicros\" : \"990000\",\n" +
        "  \"priceCurrencyCode\" : \"USD\",\n" +
        "  \"startTimeMillis\" : \"1477333413210\"\n" +
        "}");
return new MockHttpTransport.Builder().setLowLevelHttpResponse(mockResponse).build();

当我使用模拟类运行测试时,我得到以下堆栈跟踪:

When I run the test with the mock classes I get the following stacktrace:

java.lang.IllegalArgumentException: no JSON input found
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:125)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:49)
at com.google.api.client.json.JsonParser.startParsing(JsonParser.java:223)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:380)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:355)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:459)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

我想念什么?如何模拟OAuth2响应以及获取订阅的调用?

What am I missing? How does one mock out the OAuth2 response as well as the call to get the subscription?

推荐答案

我能够弄清楚我的问题.除了模拟HttpTransport外,我还必须提供模拟的GoogleCredential,例如:

I was able to figure out my issue. In addition to mocking out the HttpTransport I also had to provide a mock GoogleCredential like:

new MockGoogleCredential.Builder().build();

这篇关于如何模拟Google API AndroidPublisher请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆