vendredi 22 novembre 2019

How to mock android framework class for service testing?

I have ServiceA which is responsible for catching data from CarPropertyManager, then doing some operations on this data and forwarding to binded clients. Simplified version of my service:

class ServiceA extends Service {
    ...
    IBinder onBind(Intent intent) {
        mCarApi = Car.createCar(..., mCarServiceConnection);
        mCarApi.connect();
        mCarPropertyManager = mCarApi.getManager(Car.PROPERTY_SERVICE);
        mCarPropertyManager.registerListener(mListener,...);
    }

    mListener = new CarPropertEventListener {
       @Override
       void onNewEvent(CarPropertyValue val) {
           sendToClients(...)
       }
    }
}

Now i want to test this service using my own implementation of Car and CarProperyManager. I want to make something like black-box tests:

  • Bind clients to ServiceA (Using serviceTestRule)
  • Send some values from CarPropertyManager
  • Catch those values
  • Check are they correct

I have placed CarPropertyManager and Car in packages named same as original ones. Now when i want to run those tests, I have to rename packages for example from android.car.hardware to android.car2.hardware. How to make that tests will use my implementation of those classes? ( I have to say, that I am running those tests on platform using adb, not in AndroidStudio)

Aucun commentaire:

Enregistrer un commentaire