How can I unit test an Android Activity that acts on Accelerometer?

后端 未结 5 1403
心在旅途
心在旅途 2021-01-03 04:00

I am starting with an Activity based off of this ShakeActivity and I want to write some unit tests for it. I have written some small unit tests for Android activities befor

5条回答
  •  死守一世寂寞
    2021-01-03 04:18

    How can I send fake data to the accelerometer from a unit test?

    AFAIK, you can't.

    Have your shaker logic accept a pluggable data source. In the unit test, supply a mock. In production, supply a wrapper around the accelerometer.

    Or, don't worry about unit testing the shaker itself, but rather worry about unit testing things that use the shaker, and create a mock shaker.

提交回复
热议问题