Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write unit test for onTouch() android

Tags:

android

junit

I am having a text field in my project and associated setOnTouchListener with that. In onTouch() I am performing the intended action after clicking on that text field.

I need to write unit test case for onTouch(). Can anyone help me?

    public boolean onTouch(View v, MotionEvent event) { 

        final Intent intent;

        switch (v.getId()) { 

            case R.id.xyz: 
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(getString(R.string.xyz))); 
                startActivity(intent); 
                break;

            case R.id.abc: 
                intent = new Intent(Intent.ACTION_VIEW); 
                intent.setData(Uri.parse(getString(R.string.abc))); 
                startActivity(intent); 
                break; 

            default: 
                break;

        } 

        return false; 
}
like image 226
Meghana M Avatar asked Jul 14 '26 17:07

Meghana M


1 Answers

So the method i found was :

  • get required View from the intended activity using findViewById
  • Create object of that activity
  • call onTouch(view,null)
  • no need to take care regarding motion Event's object because its not getting used in this unit test
like image 177
Meghana M Avatar answered Jul 17 '26 20:07

Meghana M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!