如何使用mockito获取位图资源?

时间:2017-09-18 01:19:37

标签: java android unit-testing mockito

我的类需要一个位图,但mockito的模拟上下文对象似乎无法做到这一点:

    public class PlayerTest {

        @Mock // Mocking the android context class
        Context mMockContext;
        // intantiating object from that mocked class
        Context mContext;

        private Player player;
        private Bitmap playerBitmap;

        private int screenX;
        private int screenY;

        @Before
        public void setupPlayer(){
            playerBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.player);
            player = new Player(mContext, screenX, screenY,playerBitmap);
        }
...

1 个答案:

答案 0 :(得分:4)

您是否只能使用private Bitmap playerBitmap注释@Mock

我无法看到课程的其余部分,但我认为您正在使用Mockito JUnit跑步者。如果没有,您需要使用它,或者在设置方法中拨打MockitoAnnotations.initMocks(this)