Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a bitmap resource with mockito?

My class needs a bitmap but mockito's mock context object is not capable of doing that is seems:

    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);
        }
...
like image 314
red888 Avatar asked Mar 08 '23 01:03

red888


1 Answers

Can't you just annotate private Bitmap playerBitmap with @Mock?

I can't see the rest of the class, but I presume you're using the Mockito JUnit runner. If not, you'll need to either use that, or call MockitoAnnotations.initMocks(this) in your setup method.

like image 131
ThrawnCA Avatar answered Mar 20 '23 11:03

ThrawnCA