Skip to content

Instantly share code, notes, and snippets.

@thomi137
Created November 4, 2013 14:12
Show Gist options
  • Save thomi137/7303023 to your computer and use it in GitHub Desktop.
Save thomi137/7303023 to your computer and use it in GitHub Desktop.
Android JUnit sample code for the Tricentis Accelerate Conference 7. November 2013
/**
* CensusAppTest
* com.newthinktank.censusapp.test
* ContactListActivityTest.java
* TODO
* Copyright (c) 30.10.2013, 2013 t.prosser
* Description:
*/
package com.newthinktank.censusapp.test;
import java.util.ArrayList;
import java.util.List;
import com.newthinktank.censusapp.AllContacts;
import com.newthinktank.censusapp.Contact;
import com.newthinktank.censusapp.ContactListActivity;
import com.newthinktank.censusapp.FragmentContactList;
import android.support.v4.app.Fragment;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/**
* @author t.prosser
*
*/
public class ContactListActivityTest extends
ActivityInstrumentationTestCase2<ContactListActivity> {
// Setup logging
private static final String TEST_LOG_TAG = "Contact List Test";
// Setup static data
private static final int TEST_NUM_LIST_ITEMS = 3;
private ContactListActivity mFirstTestActivity;
private List<Fragment> mFragments;
private FragmentContactList mFragmentContactList;
private List<Contact> mAllContacts;
private View mView;
/**
* @param activityClass
*/
public ContactListActivityTest() {
super(ContactListActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mFirstTestActivity = getActivity();
mFragments = new ArrayList<Fragment>(mFirstTestActivity.getSupportFragmentManager().getFragments());
mFragmentContactList = (FragmentContactList) mFragments.get(0);
mAllContacts = new ArrayList<Contact>(AllContacts.get(mFirstTestActivity).getContactList());
}
public void testPreconditions() {
Log.i(TEST_LOG_TAG, "testing preconditions");
// make sure activity is loaded
assertNotNull(mFirstTestActivity);
// fragments should be loaded
assertNotNull(mFragments);
// access to the ContactList fragment
assertNotNull(mFragmentContactList);
}
public void testNumberOfListItems(){
assertEquals(TEST_NUM_LIST_ITEMS, mFragmentContactList.getListAdapter().getCount());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment