我试图创建一个简单的测试,检查列表视图的数据,直到找到相应的条目。这是ListActivity:
package com.example.unittest.app;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListActivity extends ActionBarActivity {
ListView list;
List<Map<String, String>> characterList;
SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
list = (ListView)findViewById(R.id.listView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getBaseContext(), "Item " + i , Toast.LENGTH_SHORT).show();
}
});
characterList = new ArrayList<Map<String, String>>();
initList();
adapter = new SimpleAdapter(this, characterList, android.R.layout.simple_list_item_1, new String[] {"character"}, new int[] {android.R.id.text1});
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void initList() {
characterList.add(createItem("character", "Don"));
characterList.add(createItem("character", "Sally"));
characterList.add(createItem("character", "Betty"));
characterList.add(createItem("character", "Joan"));
characterList.add(createItem("character", "Freddy"));
characterList.add(createItem("character", "Bert"));
characterList.add(createItem("character", "Roger"));
characterList.add(createItem("character", "Peggy"));
characterList.add(createItem("character", "Dawn"));
characterList.add(createItem("character", "Megan"));
characterList.add(createItem("character", "Bob"));
characterList.add(createItem("character", "Pete"));
}
private HashMap<String, String> createItem(String key, String name) {
HashMap<String, String> month = new HashMap<String, String>();
month.put(key, name);
return month;
}
}
为了找到正确的行,我试图使用onData遍历地图数组以找到正确的条目。这是我的测试课:
package com.example.unittest.test;
import android.test.ActivityInstrumentationTestCase2;
import com.example.unittest.app.MainActivity;
import com.example.unittest.app.R;
import org.hamcrest.Matchers;
import java.util.Map;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onData;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withContentDescription;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
/**
* Created by user on 2014-04-21.
*/
public class EspressoInterfaceTest extends ActivityInstrumentationTestCase2<MainActivity> {
public EspressoInterfaceTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testButton1() {
onView(withId(R.id.button)).perform(click());
onView(withId(R.id.textview)) // ViewMatcher
.check(matches(withText("Pressed!"))); // ViewAssertion
}
public void testButton2() {
onView(withId(R.id.button2))
.perform(click());
onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("character"), is("Don")))
.perform(click());
}
}
不幸的是,我认为我导入了错误的Matchers,因为我的onData和.perform方法无法解析,并显示为红色。有没有办法来解决这个问题?我的onData通话能正常工作吗?我使用https://code.google.com/p/android-test-kit/wiki/EspressoSamples作为参考,但似乎无法使其正常工作。谢谢。
最佳答案
您在执行操作之前错过了一个括号:
onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("character"), is("Don"))))
.perform(click());