#android #testing #google-api-client
#Android #тестирование #google-api-клиент
Вопрос:
Я пытаюсь протестировать приложение с помощью GoogleApiClient, но пока безуспешно. Есть ли что-то, чего мне не хватает, или невозможно протестировать GoogleAPI Client в android.test?
public class BookTest extends AndroidTestCase {
// to wait for a callack from Google Api
boolean connected = false;
public void testGoogleApiClient() {
final CountDownLatch signal = new CountDownLatch(1);
assertNotNull(getContext());
GoogleApiClient client = new GoogleApiClient.Builder(getContext())
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
connected = true;
signal.countDown();
}
@Override
public void onConnectionSuspended(int i) {
fail("should not reach here");
signal.countDown();
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
fail("should not reach here");
signal.countDown();
}
})
.build();
try {
signal.await(15, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("should not reach here");
e.printStackTrace();
}
assertTrue(connected);
}
}
сбой утверждения, подобный:
07-08 02:14:39.339 19179-19194/net.mootoh.messtin_android.app I/TestRunner﹕ failed: testGoogleApiClient(net.mootoh.messtin_android.test.BookTest)
07-08 02:14:39.339 19179-19194/net.mootoh.messtin_android.app I/TestRunner﹕ ----- begin exception -----
07-08 02:14:39.339 19179-19194/net.mootoh.messtin_android.app I/TestRunner﹕ junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:48)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertTrue(Assert.java:27)
at net.mootoh.messtin_android.test.BookTest.testGoogleApiClient(BookTest.java:58)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at junit.framework.TestResult.runProtected(TestResult.java:133)
at junit.framework.TestResult.run(TestResult.java:118)
at junit.framework.TestCase.run(TestCase.java:124)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
07-08 02:14:39.349 19179-19194/net.mootoh.messtin_android.app I/TestRunner﹕ ----- end exception -----
07-08 02:14:39.349 19179-19194/net.mootoh.messtin_android.app I/TestRunner﹕ finished: testGoogleApiClient(net.mootoh.messtin_android.test.BookTest)
07-08 02:14:39.349 19179-19194/net.mootoh.messtin_android.app I/TestRunner﹕ started: testAndroidTestCaseSetupProperly(net.mootoh.messtin_android.test.BookTest)
07-08 02:14:39.359 19179-19194/net.mootoh.messtin_android.app I/TestRunner﹕ finished: testAndroidTestCaseSetupProperly(net.mootoh.messtin_android.test.BookTest)
07-08 02:14:39.359 19179-19194/net.mootoh.messtin_android.app I/TestRunner﹕ passed: testAndroidTestCaseSetupProperly(net.mootoh.messtin_android.test.BookTest)
Это означает, что при подключении к Google Api истекает время ожидания, в то время как он успешно подключается к Google Api, если я использую фактическое действие. возможно, невозможно подключиться к Google Api с тестовым контекстом …?
Комментарии:
1. Вы не подключаете GoogleApiClient? Ошибка очистки кода?
2. Дело в том, что GoogleApiClient подключается без проблем, если используется в реальной деятельности, но не в среде тестирования (AndroidTestCase.getContext())
3. Я просто пытался немного сузить проблему, взяв низко висящий фрукт. Как написано, я ожидал бы, что этот тест завершится неудачей через 15 секунд с таймаутом каждый раз, потому что connect () никогда не вызывается. GoogleApiClient должен иметь возможность подключаться из тестового запуска просто отлично, но вам, возможно, придется поиграть с тем, какой контекст вы используете. Я думаю, вам нужен контекст тестируемого приложения, а не тестируемое приложение.
4. @mootoh, как вы выполнили свой тестовый пример? Я нахожу решение для написания тестового примера GoogleApiClient.