Android 用机器人分子测试ListView
我目前正试图用Robolectric测试一些Android代码,但我的ListView有一些问题。当我尝试访问子视图时,由于列表为空,ListView总是返回null 应用程序的实现如下所示,并创建一个简单的列表视图:Android 用机器人分子测试ListView,android,android-listview,robolectric,Android,Android Listview,Robolectric,我目前正试图用Robolectric测试一些Android代码,但我的ListView有一些问题。当我尝试访问子视图时,由于列表为空,ListView总是返回null 应用程序的实现如下所示,并创建一个简单的列表视图: private ListView listView; private ArrayAdapter<String> adapter; private static String values[] = new String[] {"Android", "A
private ListView listView;
private ArrayAdapter<String> adapter;
private static String values[] =
new String[] {"Android", "Apple", "Windows" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
initialize();
}
private void initialize() {
listView = (ListView) findViewById(R.id.tweet_list);
adapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_list_item_1,
values);
listView.setAdapter(adapter);
}
我看到你用的是旧版本的机器人分子。我们没有任何与Robolectric 1.1类似的代码问题 因此,在pom文件中添加以下行:
<dependency>
<groupId>com.pivotallabs</groupId>
<artifactId>robolectric</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
com.pivotallabs
如果有人喜欢我,仍然访问该链接以解决如何使用Roboelectric 3.0测试listview的问题。这是我的MAinActivityTest文件
private MainActivity mainActivity;
private ListView lstView;
@Before
public void setup() throws Exception{
mainActivity= Robolectric.setupActivity(MainActivity.class);
assertNotNull("Mainactivity not intsantiated",mainActivity);
lstView=(ListView)mainActivity.findViewById(R.id.list);//getting the list layout xml
ShadowLog.stream = System.out; //This is for printing log messages in console
}
@Test
public void shouldFindListView()throws Exception{
assertNotNull("ListView not found ", lstView);
ShadowListView shadowListView = Shadows.shadowOf(lstView); //we need to shadow the list view
shadowListView.populateItems();// will populate the adapter
ShadowLog.d("Checking the first country name in adapter " ,
((RowModel)lstView.getAdapter().getItem(0)).getCountry());
assertTrue("Country Japan doesnt exist", "Japan".equals(((RowModel) lstView.getAdapter().getItem(0)).getCountry()));
assertTrue(3==lstView.getChildCount());
}
RowModel是在listview中显示字段的简单POJO 如果这是你所有的测试,我就不会有了。您实际测试的数组有三个元素,第一个元素是“Android”。还有一个注释——“如果”条件可以替换为assertEquals(3,listView.getChildCount())您使用的是什么robolectric版本?我使用的是robolectric版本0.9.4。我目前正试图编写自己的ShadowArrayAdapter,但有很多问题。没有看到他们网站上的评论,他们现在正在通过Sonatype分发资源。我使用的是没有提供ArrayAdapters的旧版本…没错,我查看了源代码-所有内容都已实现。我们在这里使用的是Robolectric 1.2。是的,ShadowListView.populateItems()
是关键。谢谢
private MainActivity mainActivity;
private ListView lstView;
@Before
public void setup() throws Exception{
mainActivity= Robolectric.setupActivity(MainActivity.class);
assertNotNull("Mainactivity not intsantiated",mainActivity);
lstView=(ListView)mainActivity.findViewById(R.id.list);//getting the list layout xml
ShadowLog.stream = System.out; //This is for printing log messages in console
}
@Test
public void shouldFindListView()throws Exception{
assertNotNull("ListView not found ", lstView);
ShadowListView shadowListView = Shadows.shadowOf(lstView); //we need to shadow the list view
shadowListView.populateItems();// will populate the adapter
ShadowLog.d("Checking the first country name in adapter " ,
((RowModel)lstView.getAdapter().getItem(0)).getCountry());
assertTrue("Country Japan doesnt exist", "Japan".equals(((RowModel) lstView.getAdapter().getItem(0)).getCountry()));
assertTrue(3==lstView.getChildCount());
}