Android Unit Tests Requiring Context

后端 未结 8 1749
抹茶落季
抹茶落季 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: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 activity;
        Context mContext = null;
        ...
        @Before
        public void setUp() {
            activity = getActivity();
            mContext = activity;
        }
        ... //tests to follow
    }
    

    What does everyone else do?

提交回复
热议问题