2015-12-30 6 views
7

Wie Unit Test Android SensorEvent und MotionEvent Klassen?Wie MotionEvent und SensorEvent für Komponententests in Android simuliert?

Ich muss ein MotionEvent Objekt für Unit-Test erstellen. (Wir haben obtain Methode für MotionEvent, dass wir nach dem spöttischen können MotionEvent benutzerdefiniertes Objekt erstellen)

Für Motion Klasse, ich habe versucht, mit Mockito wie:

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

Aber folgenden Fehlern ich auf Android Studio bin immer :

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) 

die Website zu diesem Fehler erwähnte Im Anschluss an, ich habe

hinzugefügt
testOptions { 
     unitTests.returnDefaultValues = true 
    } 

auf build.gradle, aber immer noch bekomme ich diesen gleichen Fehler. Irgendeine Idee dazu?

+1

Für SensorEvent, ein Beispiel: https://medium.com/@jasonhite/testing-on-android-sensor-events-5757bd61e9b0#.nx5mgk6sq – Kikiwa

Antwort

3

Ich habe es schließlich für MotionEvent umgesetzt von 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()); 
    } 
} 

Wie kann man für SensorEvents das gleiche tun?

1

Hier ist, wie Sie ein SensorEvent für ein Beschleunigungsmesser Ereignis verspotten könnten:

private SensorEvent getAccelerometerEventWithValues(
     float[] desiredValues) throws Exception { 
    // Create the SensorEvent to eventually return. 
    SensorEvent sensorEvent = Mockito.mock(SensorEvent.class); 

    // Get the 'sensor' field in order to set it. 
    Field sensorField = SensorEvent.class.getField("sensor"); 
    sensorField.setAccessible(true); 
    // Create the value we want for the 'sensor' field. 
    Sensor sensor = Mockito.mock(Sensor.class); 
    when(sensor.getType()).thenReturn(Sensor.TYPE_ACCELEROMETER); 
    // Set the 'sensor' field. 
    sensorField.set(sensorEvent, sensor); 

    // Get the 'values' field in order to set it. 
    Field valuesField = SensorEvent.class.getField("values"); 
    valuesField.setAccessible(true); 
    // Create the values we want to return for the 'values' field. 
    valuesField.set(sensorEvent, desiredValues); 

    return sensorEvent; 
} 

ändern Sie den Typen oder Werte entsprechend Ihren Anwendungsfall.