title: How to mock MotionEvent and SensorEvent for Unit Testing in android?
body: <p>How to unit test android <code>SensorEvent</code> and <code>MotionEvent</code> classes ?</p>
  
  <p>I need to create one <code>MotionEvent</code> object for Unit testing. (We have <code>obtain</code> method for <code>MotionEvent</code> that we can use after mocking to create <code>MotionEvent</code> custom object )</p>
  
  <p>For MotionEvent class, I have tried with <code>Mockito</code> like  :</p>
  
  <pre><code>MotionEvent Motionevent = Mockito.mock(MotionEvent.class);
  </code></pre>
  
  <p>But following error I am getting on Android Studio:</p>
  
  <pre><code>java.lang.RuntimeException:
  
  Method obtain in android.view.MotionEvent not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details.
      at android.view.MotionEvent.obtain(MotionEvent.java)
  </code></pre>
  
  <p>Following the site mentioned on this error, I have added </p>
  
  <pre><code>testOptions {
          unitTests.returnDefaultValues = true
      }
  </code></pre>
  
  <p>on build.gradle , but still I am getting this same error . Any idea on this ?</p>