To put simply, what I'm trying to do is...
I'm trying to test the core logic of the perform click but not really the other functionality inside this code. Ideally, I would mock those other functions and see how the function reacts.
The reason behind mocking the functions is because the functions "getEmailFromField()" and "getPasswordFromField()" both call the "getText()" function on an EditText that was initialized in onCreate() and I believe it therefore throws a NullPointerException. Also, the ASyncTask call would probably need a mock as well.
Functionality trying to test
Edit: Forgot to mention that this is inside of LoginActivity
protected int performClick(int value) {
boolean success;
switch(value) {
case 1: //main login submit button
String email = getEmailFromField(); //these functions simply return EditText
String password = getPasswordFromField(); //fields by calling getText()
if (isValidLoginInput(email, password)) {
runLoginTask(email, password);
} else {
//do something about invalid login input
}
success = true;
break;
case 2:
...
default:
...
}
return success ? 1 : 0;
}
The switch statement just checks to see what button they pressed based on the value that's inputted and does something and will output "1" if the code runs successfully.
Maybe it's something I don't quite understand about localized tests, Android, or mocking, but I've tried a lot of different things and nothing seems to work out.
Current Test that doesn't work
@Mock
LoginActivity mockLoginActivity;
@Before
public void init() {
mockLoginActivity = mock(LoginActivity.class);
...
@Test
public void testPerformClickForLogin1() {
email = "random@email.com";
pass = "password";
when(mockLoginActivity.getEmailFromField()).thenReturn(email);
when(mockLoginActivity.getPasswordFromField()).thenReturn(pass);
when(mockLoginActivity.runLoginTask(email, pass)).thenReturn(1);
when(mockLoginActivity.isValidLoginInput(email, pass)).thenReturn(true);
when(mockLoginActivity.performClick(mockLoginActivity.BTN_CODE_SUBMIT)).thenReturn(1);
doCallRealMethod().when(mockLoginActivity.performClick(mockLoginActivity.BTN_CODE_SUBMIT));
btnResponse = mockLoginActivity.performClick(mockLoginActivity.BTN_CODE_SUBMIT);
assertThat(btnResponse, is(1));
}
Any idea on how to properly test this?
Thank you for your help.
Aucun commentaire:
Enregistrer un commentaire