2

I have a class that creates a bitmap object.

It needs access to a bitmap resource so it needs access to the context which I pass into its constructor so I call it like myClass(context)

Ideally i'd like to inject the dependency somehow. I wanted to do this with a factory so I could just ask the factory for the object and it creates it for me, but I can only get context from activity classes which create new windows. So it seems I'll have to keep passing in context from my main activity.

I asked a similar question recently and was told I shouldnt call context statically at all and should always be passing it around.

This means if I want to test this class my test class needs access to the android framework (which means I can't test locally and will have to use AndroidTests).

Is this how it is supposed to work? My class just needs a bitmap but I can't test locally now because of context.

2 Answers 2

1

If you need the functionality of Context, then you'll need to pass it into MyClass one way or another. However, this doesn't mean that you need to statically store Context. In fact, that would be a really bad design choice (looks like you already know that).

If you use Dagger, then I have this video tutorial that explains how to structure dependency injection code. Part of this structuring is indeed handling the complexity of Context objects.

Now, even if MyClass needs an access to Context in order to obtain bitmap, it doesn't necessarily mean that you need resolve to integration tests.

First of all, you can use Robolectric - this library "mocks" Android framework, and I think that it supports getting bitmap from resources.

However, depending on your use case, there is also a simpler option. Do you really need a specific bitmap, or you just want to ensure that MyClass performs specific actions on this bitmap?

If you need the specific one that you add as a resource then go for Robolectric, but if not and all you need is to make sure that the obtained bitmap is being used correctly then you can just mock the bitmap.

In order to increase readability, simplify the testing and prevent violation of Law of Demeter, I suggest that you wrap Context into this class:

public class BitmapRetriever {
    private final Context mContext;

    public BitmapRetriever(Context context) { 
        mContext = context;
    }

    public Bitmap getBitmapById(int id) {
        // code that obtains bitmap
    }
}

Then make MyClass depend on BitmapRetriever instead of Context.

Once you do this, it will be straightforward to pass mocked BitmapRetriever in test that will return mocked Bitmap when MyClass asks for it. Then you can assert various conditions on that mock.

6
  • That interesting, but how does having a separate bitmap retriever make testing easier.
    – red888
    Commented Sep 20, 2017 at 14:24
  • 1
    @red888, it gets down to Single Responsibility Principle. If your class takes in Context, then it can theoretically access all its methods. You know that it uses just one of them, but the guy who will work after you (or you yourself six months from now) will not know this. Furthermore, passing Context into any class breaks encapsulation. By wrapping it into a class that do has one single responsibility, you make it simpler to read the code and write tests, and you eliminate possibility that in the future someone will start using other Context's methods too.
    – Vasiliy
    Commented Sep 20, 2017 at 14:42
  • @red888, in short - if your class needs just bitmap, there is no reason to get it access the entire Context. It is called Law of Demeter violation.
    – Vasiliy
    Commented Sep 20, 2017 at 14:43
  • @red888, more about single responsibility principle and context here: techyourchance.com/single-responsibility-principle
    – Vasiliy
    Commented Sep 20, 2017 at 14:44
  • If I build myClass with a factory, would I still be passing context to the factory that creates the object? I guess I would have to because I can't make the factory an activity class right? Also, why are you using "final" in the context wrapper?
    – red888
    Commented Sep 22, 2017 at 4:30
1

If your class only needs a bitmap, why can't you mock a bitmap? You should test the logic in your class. You do not need to test the Context bitmap loading functionality as I'm sure that is already tested in the Android framework. You already have a great idea with the factory pattern as you could just inject a mock factory for testing by using myClass(bitmapFactory) in your app and myClass(mockFactory) in your tests.

If you're absolutely sure you need to write a test to cover the bitmap loading, then look into testing frameworks like robolectric as it can provide mock context for scenarios like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.