Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock MotionEvent and SensorEvent for Unit Testing in android?

How to unit test android SensorEvent and MotionEvent classes ?

I need to create one MotionEvent object for Unit testing. (We have obtain method for MotionEvent that we can use after mocking to create MotionEvent custom object )

For MotionEvent class, I have tried with Mockito like :

MotionEvent Motionevent = Mockito.mock(MotionEvent.class);

But following error I am getting on Android Studio:

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)

Following the site mentioned on this error, I have added

testOptions {
        unitTests.returnDefaultValues = true
    }

on build.gradle , but still I am getting this same error . Any idea on this ?

like image 764
N Kaushik Avatar asked Dec 30 '15 13:12

N Kaushik


People also ask

What is junit mocking?

What is mocking? Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

What can be mocked in Mockito?

The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called. We don't need to do anything else to this method before we can use it.

Why mock is used in junit testing?

Using Mockito greatly simplifies the development of tests for classes with external dependencies. A mock object is a dummy implementation for an interface or a class. It allows to define the output of certain method calls. They typically record the interaction with the system and tests can validate that.

What is Mockito testing in Android?

In Mockito, we mock any class using @Mock annotation. By Mocking any class, we are creating an mock object of that speicifc class. In the above code, Operators is mocked to provide dependency for Calculator class.


1 Answers

I have finally implemented it for MotionEvent by using Roboelectric

import android.view.MotionEvent;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertTrue;

import org.robolectric.RobolectricGradleTestRunner;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class ApplicationTest {

    private MotionEvent touchEvent;

    @Before
    public void setUp() throws Exception {
        touchEvent = MotionEvent.obtain(200, 300, MotionEvent.ACTION_MOVE, 15.0f, 10.0f, 0);
    }
    @Test
    public void testTouch() {
      assertTrue(15 == touchEvent.getX());
    }
}

How can we do the same thing for SensorEvents ?

like image 187
N Kaushik Avatar answered Oct 27 '22 20:10

N Kaushik