Hi there I have a simple listview I wish to test The row of each listview is represented in the xml file below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#F87217"
android:textStyle="bold"
/>
<TextView
android:id="@+id/cluster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
<TextView
android:id="@+id/school"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
<TextView
android:id="@+id/level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
<TextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
</LinearLayout>
</LinearLayout>
So i wrote a simple test code to test the textview
inside each row of the listview
however, i have error even retrieving the textview
inside the 2nd row of the listview
public class bookLocker2Test extends
ActivityInstrumentationTestCase2<TabMainActivity>
{
private Solo solo;
private Button allSchoolsButton;
private Button viewLockerButton;
public bookLocker2Test() {
super(TabMainActivity.class);
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
allSchoolsButton = (Button) solo.getCurrentActivity().findViewById(
com.example.mobilelog6.R.id.btnSoesoss);
viewLockerButton = (Button) solo.getCurrentActivity().findViewById(
com.example.mobilelog6.R.id.btnView);
}
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
public void test(){
solo.assertCurrentActivity("wrong activity", TabMainActivity.class);
}
public void testButtonClick() {
solo.clickOnText("Search/Book Lockers");
solo.clickOnButton("SOE/SOSS");
solo.clickOnButton("View Locker Selection");
solo.waitForActivity(PopulateLockerList.class);
solo.assertCurrentActivity("wrong activity2", PopulateLockerList.class);
//solo = new Solo(getInstrumentation());
//ListView mListView = (ListView)
solo.getCurrentActivity().findViewById(R.id.listViewLockers);
ListView mListView = (ListView) solo.getView(R.id.listViewLockers);
assertNotNull("ListView not allowed to be null",mListView);
int expectedCount = 10;
int actualCount = mListView.getAdapter().getCount();
assertEquals(expectedCount, actualCount);
ListView l = ( ListView )solo.getCurrentActivity().findViewById(
com.example.mobilelog6.R.id.listViewLockers);
assertNotNull("No list views!", l);
View v = l.getChildAt(1);
TextView txt =(TextView)v.findViewById(R.id.cluster);
String abc = txt.toString();
assertEquals("Incorrect label of the listview 1", "SIS-2-1-102", abc);
}
}
Error message i am getting:
junit.framework.ComparisonFailure: Incorrect label of the listview 1 expected:<[SIS-2-1-102]> but was:<[android.widget.TextView@4348a808]>
at this line:
assertEquals("Incorrect label of the listview 1", "SIS-2-1-102", abc);
Please help
Should i be getting a string
instead of android.widget.TextView@4348a808
Probably you should change this line
assertEquals("Incorrect label of the listview 1", "SIS-2-1-102", abc);
With this:
assertEquals("Incorrect label of the listview 1", "SIS-2-1-102", abc.equals("SIS-2-1-102"));
The line:
String abc = txt.toString();
Is not doing what you want to do, it is converting the TextView object to string rather than the contents of the textview. What you want is:
String abc = txt.getText().toString();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.