5

私はAndroidのテストに関する本をフォローしていて、このサンプルを実装しています。

public class TemperatureConverterActivityTests extends
        ActivityInstrumentationTestCase2<TemperatureConverterActivity> {

    private TemperatureConverterActivity mActivity;

    private EditText mCelsius;
    private EditText mFarenheit;

    public TemperatureConverterActivityTests() {
        this("TemperatureConverterActivityTests");
    }

    public TemperatureConverterActivityTests(String name) {
        super(TemperatureConverterActivity.class);
        setName(name);
    }

    protected void setUp() throws Exception {
        super.setUp();
        mActivity = getActivity();

        mCelsius = (EditText) mActivity
                .findViewById(org.mighter.tc.R.id.celsius);
        mFarenheit = (EditText) mActivity
                .findViewById(org.mighter.tc.R.id.farenheit);
    }

    protected void tearDown() throws Exception {
    }

    public final void testPreconditions() {
        assertNotNull(mActivity);
    }

    public final void testHasInputFields() {
        assertNotNull(mCelsius);
        assertNotNull(mFarenheit);
    }

}

ただし、テストは終了しません。

コンソールに表示されるもの:

Android Launch!
adb is running normally.
Performing android.test.InstrumentationTestRunner JUnit launch
Automatic Target Mode: Several compatible targets. Please select a target device.
Uploading TemperatureConverterTest.apk onto device '80A358103041548074'
Installing TemperatureConverterTest.apk...
Success!
Project dependency found, installing: TemperatureConverter
Application already deployed. No need to reinstall.
Launching instrumentation android.test.InstrumentationTestRunner on device 80A358103041548074
Collecting test information
Test run failed: Instrumentation run failed due to 'Process crashed.'
Test run finished
Sending test information to Eclipse
Running tests...

そして、ハングします。これがJUnitビューです。

ここに画像の説明を入力してください

LogCatからの唯一の情報:

05-28 17:23:54.952: W/ActivityManager(224): Crash of app org.mighter.tc running instrumentation ComponentInfo{org.mighter.tc.test/android.test.InstrumentationTestRunner}
05-28 17:23:55.042: I/TestRunner(7174): <!>android.test.TestPrinter 50<!> started: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.062: I/TestRunner(7174): <!>android.test.TestPrinter 56<!> finished: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.062: I/TestRunner(7174): <!>android.test.TestPrinter 77<!> passed: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.072: I/TestRunner(7174): <!>android.test.TestPrinter 50<!> started: testPreconditions(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.082: I/TestRunner(7174): <!>android.test.TestPrinter 56<!> finished: testPreconditions(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.082: I/TestRunner(7174): <!>android.test.TestPrinter 77<!> passed: testPreconditions(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.652: I/TestRunner(7194): <!>android.test.TestPrinter 50<!> started: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.853: I/TestRunner(7194): <!>android.test.TestPrinter 56<!> finished: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.853: I/TestRunner(7194): <!>android.test.TestPrinter 77<!> passed: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.853: I/TestRunner(7194): <!>android.test.TestPrinter 50<!> started: testPreconditions(org.mighter.tc.test.TemperatureConverterActivityTests)

デバッグを試みましたが、testPreconditions()に到達しません。ありがとう。

4

1 に答える 1

2

tearDown();でスーパーを呼び出す必要があります。

于 2012-07-11T12:57:02.877 に答える