java - How do I locally test functionality in Android by mocking functions within that functionality, like getText() of EditText? -


to put simply, i'm trying is...

i'm trying test core logic of perform click not other functionality inside code. ideally, mock other functions , see how function reacts.

the reason behind mocking functions because functions "getemailfromfield()" , "getpasswordfromfield()" both call "gettext()" function on edittext initialized in oncreate() , believe therefore throws nullpointerexception. also, asynctask call need mock well.

functionality trying test

edit: forgot mention inside of loginactivity

protected int performclick(int value) {     boolean success;     switch(value) {         case 1: //main login submit button             string email = getemailfromfield(); //these functions return edittext              string password = getpasswordfromfield(); //fields calling gettext()             if (isvalidlogininput(email, password)) {                 runlogintask(email, password);             } else {                 //do invalid login input             }              success = true;             break;         case 2:             ...         default:             ...     }     return success ? 1 : 0; } 

the switch statement checks see button pressed based on value that's inputted , , output "1" if code runs successfully.

maybe it's don't quite understand localized tests, android, or mocking, i've tried lot of different things , nothing seems work out.

current test doesn't work

edit: using mockito

@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 test this?

thank help.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -