Edgar Da Silva Fernandes Edgar Da Silva Fernandes - 1 year ago 56
Java Question

Dagger2 + Mockito: How to Unit Test this scenario?

I'm working on a team project which has a very coupled code. I'm trying to increase the unit test coverage and I've faced the following scenario:

class Foo {
static void methodA () {
Bar b = new Bar();

class Bar {

DBAcessor mDBAcessor;

Bar () {

public Object getContent() {


I want to unit test
, however I don't know if it is possible to do it without passing the
object via constructor to
should be a POJO model being widely used throughout the project, hence, I don't know if it would be a good idea to change its constructor and impact so many other classes. Any tips on how should I approach this scenario?

Answer Source

That is wrong design.

Whenever you have an access to the constructor, pass class dependencies into constructor. That really eases unit testing. Class Bar should look:

class Bar() {

    @Inject DBAcessor mDBAcessor;

    Bar (DBAcessor dbAcessor) {
        this.mDBAcessor = dbAcessor;

    public Object getContent() {

Dagger2 methods declared in Component should only be used inside object you don't have an access to the constructor (Activity or Fragment).

Then nothing prevents you from doing that:

DBAccessor dbAccessor = Mockito.mock(DBAccessor.class);
Bar bar = new Bar(dbAccessor);
verify(bar, times(1)).fetchData();

If you really need to stick to such design, then you can't make unit tests on such object, as Dagger2 depends on Application object, which needs Context, which you cannot emulate in unit tests. Write your test as androidTest, create appropriate Activity, fetch Bar object from it and test it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download