Dagger2 + Mockito: How to Unit Test this scenario?

时光怂恿深爱的人放手 提交于 2019-12-25 07:59:29

问题


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();
        b.getContent();
    }
}

class Bar {

    @Inject
    DBAcessor mDBAcessor;

    Bar () {
        Dagger2.getInjector().inject(this);
    }

    public Object getContent() {
        mDBAcessor.fetchData();
    }

}

I want to unit test methodA(), however I don't know if it is possible to do it without passing the DBAcessor object via constructor to Bar() class. Bar 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?


回答1:


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() {
        mDBAcessor.fetchData();
    }
}

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);
bar.getContent();
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.



来源:https://stackoverflow.com/questions/40286376/dagger2-mockito-how-to-unit-test-this-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!