在AndroidTestCase下测试GoogleApiClient

时间:2014-07-07 16:45:56

标签: android testing google-api-client

我尝试使用GoogleApiClient测试应用,但到目前为止还没有运气。我有什么遗漏,或者无法在android.test下测试GoogleApi客户端吗?

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 ......?

0 个答案:

没有答案