I am working on a test class for an android app and I'm getting an android.view.ViewRootImpl$CalledFromWrongThreadException. The code is suppose to simulate an input into a text field. This exception is thrown in the method testSearch() when the statement searchText.setText("hi");is called. I don't know why it does that and I would like to fix it. Here is the code:
package com.example.guy.smsclassproject;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
/**
* Created by Guy on 11/16/2015.
*/
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);
}
@Override
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();
tester = getActivity();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
searchText = (EditText) tester.findViewById(R.id.searchText);
searchButton = (Button) tester.findViewById(R.id.searchButton);
}
@SmallTest
public void testSearch() {
//The problem occurs here:
searchText.setText("hi");
searchButton.performClick();
messagesToBeDisplayed = draftsDatabase.getMessagesByKey(searchText.getText().toString());
assertEquals("Messages with the word hi", 2, messagesToBeDisplayed.size());
}
}
Aucun commentaire:
Enregistrer un commentaire