Android Unit Tests Requiring Context

后端 未结 8 1745
抹茶落季
抹茶落季 2020-12-13 23:47

I am writing my first Android database backend and I\'m struggling to unit test the creation of my database.

Currently the problem I am encountering is obtaining a va

相关标签:
8条回答
  • 2020-12-14 00:17

    You should use ApplicationTestCase or ServiceTestCase.

    0 讨论(0)
  • 2020-12-14 00:20

    Using the AndroidTestCase:getContext() method only gives a stub Context in my experience. For my tests, I'm using an empty activity in my main app and getting the Context via that. Am also extending the test suite class with the ActivityInstrumentationTestCase2 class. Seems to work for me.

    public class DatabaseTest extends ActivityInstrumentationTestCase2<EmptyActivity>
        EmptyActivity activity;
        Context mContext = null;
        ...
        @Before
        public void setUp() {
            activity = getActivity();
            mContext = activity;
        }
        ... //tests to follow
    }
    

    What does everyone else do?

    0 讨论(0)
提交回复
热议问题