In the past few days I started playing around with roboguice, robolectric and mockito. I have a small Android-application with a login-screen containing an AutoCompleteTextView for faster entering the username. The usernames for the AutoCompleteTextView are stored in a sqlite-database.
public class MainActivity extends RoboActivity implements View.OnClickListener {
@InjectView(R.id.startScreen_Login_Button) private Button loginButton;
@InjectView(R.id.startScreen_Cancel_Button) private Button cancelButton;
@InjectView(R.id.startScreen_forgotPwd_TextView) private TextView forgotPWTextView;
@InjectView(R.id.startScreen_Username_AutoCompleteTextView) private AutoCompleteTextView loginUsernameAutoCompleteTextView;
@InjectView(R.id.startScreen_Password_EditText) private EditText loginPasswordEditText;
@Inject private SharedPreferences sharedPreferences;
@Inject SQLiteDBAdapter dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loginButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
forgotPWTextView.setOnClickListener(this);
// Creating List for startScreen_Username_AutoCompleteTextView
List<User> userList = dbAdapter.getUserList();
ListIterator<User> it = userList.listIterator();
List<String> userStringList = new ArrayList<String>();
User user;
while (it.hasNext()) {
user = it.next();
userStringList.add(user.getName());
}
loginUsernameAutoCompleteTextView.setAdapter(new ArrayAdapter<String>(this, R.layout.select_page_row, userStringList));
}
...
}
I want to test MainActivity using robolectric, trying to mock the database with mockito. This is my test-class:
@RunWith(CustomRobolectricTestRunner.class)
public class MainActivityTest {
@Mock
SQLiteDBAdapter dbAdapter;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void shouldHaveApplicationName() throws Exception {
String appName = new MainActivity().getResources().getString(R.string.app_name);
assertThat(appName, equalTo("OperationReport"));
}
@Test
public void testButtonsVisible()
{
MainActivity mainActivity = new MainActivity();
mainActivity.onCreate(null);
}
}
Calling mainActivity.onCreate(null); is starting the error-cascade, ending in line Cursor cursor = db.rawQuery(SQL_QUERY, null); of my getUserList-method in my SQLiteDBAdapter:
public List<User> getUserList() {
SQLiteDatabase db = getReadableDatabase();
List<User> userList = new ArrayList<User>();
String SQL_QUERY = "SELECT * FROM User;";
Cursor cursor = db.rawQuery(SQL_QUERY, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
User user = new User();
user.setUserUUID(cursor.getString(0));
user.setName(cursor.getString(1));
user.setPassword(cursor.getString(2));
user.setDateOfBirth(cursor.getString(3));
user.setStaffNumber(cursor.getString(4));
user.setActive(cursor.getInt(5));
user.setUserClass(cursor.getInt(6));
userList.add(user);
cursor.moveToNext();
}
cursor.close();
db.close();
return userList;
}
I read, that a Mock is returning empty stubs of void-methods, and returns null on any other method. As I am mocking the SQLiteDBAdapter-class I am expecting that calling getUserList on my mocked SQLiteDBAdapter returns null. It is not quite clear to me, why he is accessing the original method. I guess it is still using the original SQLiteDBAdapter and not the Mock. What do I have to do to fix this, and how is it working? I ran out of ideas, so any help is appreciated.