ActivityInstrumentationTestCase2

ActivityInstrumentationTestCase2

我希望我的应用程序使用 ActivityInstrumentationTestCase2 在代码中执行以下测试。问题是 getActivity() 方法返回空值。这会在包含 getActivity() 的行正下方的行中导致 NullPointerException。因此,测试无法运行。我不知道它为什么这样做。我已经尝试将 @Before 更改为 @Override 但问题仍然存在。

这是实际的测试类:

package com.example.guy.smsclassprojecttest;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.Button;
import android.widget.EditText;

import com.example.guy.smsclassproject.*;

import org.junit.*;
import org.junit.Test;

import java.util.ArrayList;


public class DraftsActivityTest2 extends ActivityInstrumentationTestCase2<DraftsActivity>
{
    private EditText searchText;
    private Button searchButton;
    private DraftsDatabase draftsDatabase;
    ArrayList<MessageObject> messagesToBeDisplayed;
    DraftsActivity tester;

    public DraftsActivityTest2()
    {
        super(DraftsActivity.class);

    }

    @Before
    public void setUp() throws Exception
    {

        draftsDatabase = new DraftsDatabase();
        MessageObject messageObject1 = new MessageObject("hi", "5554", true);
        MessageObject messageObject2 = new MessageObject("hi hi", "5555554", true);
        MessageObject messageObject3 = new MessageObject("sup", "5435555554", true);
        draftsDatabase.addMessage(messageObject1);
        draftsDatabase.addMessage(messageObject2);
        draftsDatabase.addMessage(messageObject3);
        messagesToBeDisplayed = draftsDatabase.getAllTexts();
        //Here is where the code crashes
        tester = getActivity();
        messagesToBeDisplayed = tester.messagesToBeDisplayed;
        searchText = (EditText) tester.findViewById(R.id.searchText);
        searchButton = (Button) tester.findViewById(R.id.searchButton);
    }

    @Test
    public void testSearch() {

        searchText.setText("hi");
        searchButton.performClick();
        messagesToBeDisplayed = draftsDatabase.getMessagesByKey(searchText.getText().toString());
        Assert.assertEquals("Messages with the word hi", 2, messagesToBeDisplayed.size());
        Assert.assertTrue(false);
    }
}

如果它们有所不同,这是 list 文件和 gradle 文件:

list 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.guy.smsclassproject" >

    <uses-sdk android:minSdkVersion="10" />
    android:versionCode="1"
    android:versionName="1.0">
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="SMS"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <!-- main menu -->
        <activity android:name=".MenuActivity" android:label="SMS">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Drafts -->
        <activity android:name=".DraftsActivity"
            android:label="Drafts"
            android:windowSoftInputMode="adjustPan"> <!-- stops keyboard from messing up layout -->
        </activity>

        <!-- History -->
        <activity
            android:name=".HistoryActivity"
            android:label="Recent Texts"
            android:windowSoftInputMode="adjustPan"> <!-- stops keyboard from messing up layout -->
        </activity>

        <!-- Texts -->
        <activity
            android:name=".TextingActivity"
            android:label="Texting"
            android:windowSoftInputMode="adjustPan" > <!-- stops keyboard from messing up layout -->
        </activity>

        <receiver android:name=".SmsReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".SingleTextActivity"
            android:label="Message" >//http://stackoverflow.com/questions/2198410/how-to-change-title-of-activity-in-android--Tian
        </activity>
    </application>

</manifest>

Gradle :
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testApplicationId 'com.example.guy.smsclassprojecttest'


    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    testOptions {

        unitTests.returnDefaultValues = true

    }

    defaultConfig
    {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    testCompile 'junit:junit:4.12'
}

最佳答案

如果您使用的是 ActivityInstrumentationTestCase2,则不需要 @Before、@Test 批注。

有2种方法:

  • 继续使用 ActivityInstrumentationTestCase2 来测试这个 Activity,从你的代码中删除所有 @Before @Test 注释,测试用例应该可以正常运行。
  • 不要继承ActivityInstrumentationTestCase2,而是使用AndroidJUnitRunner,并通过像这样设置 Activity 测试规则来设置Activity


  • public class DraftsActivityTest2 {
    
    @Rule
    public ActivityTestRule<DraftsActivity> mActivityRule = new ActivityTestRule<>(
            DraftsActivity.class);
    
    @Before
    public void setUp(){
        DraftsActivity activity = mActivityRule.getActivity();
        ...}
    }
    

    关于android - getActivity() 在我的 ActivityInstrumentationTestCase2 类中返回一个 null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33727849/

    10-12 06:11