我正在尝试使用robotium执行单元测试...每次尝试时都会出现空指针异常...一切都很好,直到应用程序加载,然后发生异常...在本教程中,据说会发生此错误但是解决方案还不清楚..请帮助..代码如下:

package com.testCalculator;
import android.test.ActivityInstrumentationTestCase2;
import com.jayway.android.robotium.solo.Solo;

@SuppressWarnings("unchecked")
public class TestApk extends ActivityInstrumentationTestCase2 {
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.calculator.Main";

    private static Class launcherActivityClass;
    static {
        try {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        }
        catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public TestApk() throws ClassNotFoundException {
        super(launcherActivityClass);
    }

    private Solo solo;
    @Override
    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testDisplayBlackBox() {
        // Enter any integer/decimal value for first edit-field, we are writing 10
        solo.clearEditText(0);
        solo.enterText(0, "10");
        // Enter any integer/decimal value for first edit-field, we are writing 20
        solo.clearEditText(1);
        solo.enterText(1, "20");
        // Tap on Multiply button
        solo.clickOnButton("Multiply");
        //Assert that the correct resultant of 10 x 20 is returned
        assertTrue("Problem asserting multiply", solo.searchText("200"));
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
    }
}


黑盒测试失败跟踪:

TestApk (2)
samsung-samsung_sm_n900a-a0964e35
com.testCalculator.TestApk
testDisplayBlackBox (com.testCalculator.TestApk) [samsung-samsung_sm_n900a-a0964e35]
java.lang.NullPointerException

at com.jayway.android.robotium.solo.ViewFetcher.getNonDecorViews(ViewFetcher.java:163)

at com.jayway.android.robotium.solo.ViewFetcher.getAllViews(ViewFetcher.java:85)

at com.jayway.android.robotium.solo.ViewFetcher.getViews(ViewFetcher.java:194)

at com.jayway.android.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:312)

at com.jayway.android.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:299)

at com.jayway.android.robotium.solo.Searcher.searchFor(Searcher.java:123)

at com.jayway.android.robotium.solo.Waiter.waitForView(Waiter.java:105)

at com.jayway.android.robotium.solo.Waiter.waitForAndGetView(Waiter.java:348)

at com.jayway.android.robotium.solo.Solo.enterText(Solo.java:1290)

at com.testCalculator.TestApk.testDisplayBlackBox(TestApk.java:29)

at java.lang.reflect.Method.invokeNative(Native Method)

at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)

at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)

at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)

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:1741)


日志猫输出:

07-18 10:36:28.084: I/dalvikvm(31894): Enabling JNI app bug workarounds for target SDK version 8...
07-18 10:36:28.134: I/TestRunner(31894): started: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:28.154: I/TestRunner(31894): finished: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:28.154: I/TestRunner(31894): passed: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:28.924: I/dalvikvm(31977): Enabling JNI app bug workarounds for target SDK version 8...
07-18 10:36:28.964: I/TestRunner(31977): started: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:29.014: W/ApplicationPackageManager(31977): getCSCPackageItemText()
07-18 10:36:29.124: I/Adreno-EGL(31977): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
07-18 10:36:29.124: I/Adreno-EGL(31977): OpenGL ES Shader Compiler Version: E031.24.00.07
07-18 10:36:29.124: I/Adreno-EGL(31977): Build Date: 01/22/14 Wed
07-18 10:36:29.124: I/Adreno-EGL(31977): Local Branch: base_au149_adreno_au169_patches
07-18 10:36:29.124: I/Adreno-EGL(31977): Remote Branch:
07-18 10:36:29.124: I/Adreno-EGL(31977): Local Patches:
07-18 10:36:29.124: I/Adreno-EGL(31977): Reconstruct Branch:
07-18 10:36:29.164: D/OpenGLRenderer(31977): Enabling debug mode 0
07-18 10:36:29.254: V/RenderScript(31977): Application requested CPU execution
07-18 10:36:29.254: V/RenderScript(31977): 0x78878260 Launching thread(s), CPUs 4
07-18 10:36:29.274: E/dalvikvm(31977): Could not find class 'android.support.v4.app.FragmentActivity', referenced from method com.jayway.android.robotium.solo.Waiter.getSupportFragment
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to resolve check-cast 21 (Landroid/support/v4/app/FragmentActivity;) in Lcom/jayway/android/robotium/solo/Waiter;
07-18 10:36:29.274: D/dalvikvm(31977): VFY: replacing opcode 0x1f at 0x0009
07-18 10:36:29.274: I/dalvikvm(31977): Could not find method android.support.v4.app.FragmentActivity.getSupportFragmentManager, referenced from method com.jayway.android.robotium.solo.Waiter.getSupportFragment
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to resolve virtual method 26: Landroid/support/v4/app/FragmentActivity;.getSupportFragmentManager ()Landroid/support/v4/app/FragmentManager;
07-18 10:36:29.274: D/dalvikvm(31977): VFY: replacing opcode 0x6e at 0x0010
07-18 10:36:29.274: I/dalvikvm(31977): Could not find method android.support.v4.app.FragmentActivity.getSupportFragmentManager, referenced from method com.jayway.android.robotium.solo.Waiter.getSupportFragment
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to resolve virtual method 26: Landroid/support/v4/app/FragmentActivity;.getSupportFragmentManager ()Landroid/support/v4/app/FragmentManager;
07-18 10:36:29.274: D/dalvikvm(31977): VFY: replacing opcode 0x6e at 0x0019
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to find class referenced in signature (Landroid/support/v4/app/Fragment;)
07-18 10:36:29.274: W/dalvikvm(31977): VFY: unable to find class referenced in signature (Landroid/support/v4/app/Fragment;)
07-18 10:36:29.774: W/System.err(31977): java.lang.NoSuchFieldException: mViews
07-18 10:36:29.784: W/System.err(31977):    at java.lang.Class.getDeclaredField(Class.java:596)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getWindowDecorViews(ViewFetcher.java:382)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getAllViews(ViewFetcher.java:83)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getViews(ViewFetcher.java:194)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:312)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:299)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.android.robotium.solo.Searcher.searchFor(Searcher.java:123)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.android.robotium.solo.Waiter.waitForView(Waiter.java:105)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.android.robotium.solo.Waiter.waitForAndGetView(Waiter.java:348)
07-18 10:36:29.784: W/System.err(31977):    at com.jayway.android.robotium.solo.Solo.clearEditText(Solo.java:1339)
07-18 10:36:29.784: W/System.err(31977):    at com.testCalculator.TestApk.testDisplayBlackBox(TestApk.java:27)
07-18 10:36:29.784: W/System.err(31977):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 10:36:29.794: W/System.err(31977):    at java.lang.reflect.Method.invoke(Method.java:515)
07-18 10:36:29.794: W/System.err(31977):    at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
07-18 10:36:29.794: W/System.err(31977):    at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
07-18 10:36:29.794: W/System.err(31977):    at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestCase.runBare(TestCase.java:134)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestResult$1.protect(TestResult.java:115)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestResult.runProtected(TestResult.java:133)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestResult.run(TestResult.java:118)
07-18 10:36:29.794: W/System.err(31977):    at junit.framework.TestCase.run(TestCase.java:124)
07-18 10:36:29.794: W/System.err(31977):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
07-18 10:36:29.794: W/System.err(31977):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
07-18 10:36:29.794: W/System.err(31977):    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
07-18 10:36:29.794: W/System.err(31977):    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)
07-18 10:36:29.994: W/IInputConnectionWrapper(31977): showStatusIcon on inactive InputConnection
07-18 10:36:30.734: I/TestRunner(31977): failed: testDisplayBlackBox(com.testCalculator.TestApk)
07-18 10:36:30.734: I/TestRunner(31977): ----- begin exception -----
07-18 10:36:30.734: I/TestRunner(31977): java.lang.NullPointerException
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getNonDecorViews(ViewFetcher.java:163)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getAllViews(ViewFetcher.java:85)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getViews(ViewFetcher.java:194)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:312)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.android.robotium.solo.ViewFetcher.getCurrentViews(ViewFetcher.java:299)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.android.robotium.solo.Searcher.searchFor(Searcher.java:123)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.android.robotium.solo.Waiter.waitForView(Waiter.java:105)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.android.robotium.solo.Waiter.waitForAndGetView(Waiter.java:348)
07-18 10:36:30.734: I/TestRunner(31977):    at com.jayway.android.robotium.solo.Solo.clearEditText(Solo.java:1339)
07-18 10:36:30.734: I/TestRunner(31977):    at com.testCalculator.TestApk.testDisplayBlackBox(TestApk.java:27)
07-18 10:36:30.734: I/TestRunner(31977):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 10:36:30.734: I/TestRunner(31977):    at java.lang.reflect.Method.invoke(Method.java:515)
07-18 10:36:30.734: I/TestRunner(31977):    at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
07-18 10:36:30.734: I/TestRunner(31977):    at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
07-18 10:36:30.734: I/TestRunner(31977):    at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestCase.runBare(TestCase.java:134)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestResult$1.protect(TestResult.java:115)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestResult.runProtected(TestResult.java:133)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestResult.run(TestResult.java:118)
07-18 10:36:30.734: I/TestRunner(31977):    at junit.framework.TestCase.run(TestCase.java:124)
07-18 10:36:30.734: I/TestRunner(31977):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
07-18 10:36:30.734: I/TestRunner(31977):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
07-18 10:36:30.734: I/TestRunner(31977):    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
07-18 10:36:30.734: I/TestRunner(31977):    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)
07-18 10:36:30.734: I/TestRunner(31977): ----- end exception -----
07-18 10:36:30.734: I/TestRunner(31977): finished: testDisplayBlackBox(com.testCalculator.TestApk)


控制台输出:

Android Launch!
[2014-07-18 10:36:09 - TestCalculator] adb is running normally.
[2014-07-18 10:36:09 - TestCalculator] Performing android.test.InstrumentationTestRunner JUnit launch
[2014-07-18 10:36:09 - TestCalculator] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2014-07-18 10:36:11 - TestCalculator] Uploading TestCalculator.apk onto device 'a0964e35'
[2014-07-18 10:36:11 - TestCalculator] Installing TestCalculator.apk...
[2014-07-18 10:36:16 - TestCalculator] Success!
[2014-07-18 10:36:16 - TestCalculator] Project dependency found, installing: AndroidCalculator
[2014-07-18 10:36:16 - AndroidCalculator] Uploading AndroidCalculator.apk onto device 'a0964e35'
[2014-07-18 10:36:16 - AndroidCalculator] Installing AndroidCalculator.apk...
[2014-07-18 10:36:27 - AndroidCalculator] Success!
[2014-07-18 10:36:27 - TestCalculator] Launching instrumentation android.test.InstrumentationTestRunner on a0964e35
[2014-07-18 10:36:28 - TestCalculator] Sending test information to Eclipse
[2014-07-18 10:36:31 - TestCalculator] Test run finished

最佳答案

您正在使用旧版本的Robotium。请更新至5.2.1,您将不会遇到此问题。

https://code.google.com/p/robotium/wiki/Downloads?tm=2

关于java - 空指针异常-Robotium单元测试,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24828722/

10-10 02:41