Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getActivity() returns a null in my ActivityInstrumentationTestCase2 class

I want my app to perform the following test in the code with ActivityInstrumentationTestCase2. The problem is the getActivity() method returns a null. This results in a NullPointerException at the line directly underneath the line that contains getActivity(). As a result, the test cannot run. I don't know why it is doing that. I have already tried changing @Before to @Override but the problem continues.

Here is the actual Test class:

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);
    }
}

Here is the manifest file and the gradle file if they make a difference:

Manifest File:

<?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'
}
like image 238
Oshyrath Avatar asked Nov 16 '15 02:11

Oshyrath


1 Answers

If you are using ActivityInstrumentationTestCase2, you don't need the @Before, @Test annotations.

There are 2 ways to go:

  1. Continue to use ActivityInstrumentationTestCase2 to test this Activity, remove all the @Before @Test annotations from your code, and the test case should run without a problem.
  2. Don't subclass ActivityInstrumentationTestCase2, instead, to use AndroidJUnitRunner, and set the Activity by setting the activity test rule like this

Test case under JUnit4:

public class DraftsActivityTest2 {

@Rule
public ActivityTestRule<DraftsActivity> mActivityRule = new ActivityTestRule<>(
        DraftsActivity.class);

@Before
public void setUp(){
    DraftsActivity activity = mActivityRule.getActivity();
    ...}
}
like image 63
Eric Liu Avatar answered Oct 04 '22 19:10

Eric Liu