DaggerMock没有在Espresso测试中注入Mock对象

时间:2017-05-13 12:33:52

标签: android unit-testing dagger-2

尝试在Espresso测试中运行DaggerMock的尝试失败。我几乎完全复制了样本,但它总是使用MainPresenter的实际实现,而不是模拟实现。

EspressoRule(完全从real world sample复制):

public class EspressoDaggerMockRule extends DaggerMockRule<AppComponent>
{
    public EspressoDaggerMockRule()
    {
        super(AppComponent.class, new AppModule(getApp()));
        set(new ComponentSetter<AppComponent>()
        {
            @Override
            public void setComponent(AppComponent appComponent)
            {
                getApp().setComponent(appComponent);
            }
        });
    }

    private static App getApp()
    {
        return (App) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
    }
}

Espresso测试:

public class MainActivityTest
{
    @Rule
    public EspressoDaggerMockRule rule = new EspressoDaggerMockRule();

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class, false, false);

    @Mock MainPresenter presenter;

    @Test
    public void test() throws InterruptedException
    {
        final MainActivity activity = mActivityTestRule.launchActivity(null);
        // presenter in this test class will be mocked but the presenter in activity will be real

        Thread.sleep(1000);
    }
}

MainActivity:

public class MainActivity extends BaseActivity implements MainContract.View
{
    @Inject MainPresenter mainPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void setupComponent(AppComponent appComponent)
    {
        appComponent
                .mainActivityComponentBuilder()
                .mainActivityModule(new MainModule(this))
                .build()
                .inject(this);
    }
}

MainComponent:

@Subcomponent(modules = {MainModule.class})
public interface MainComponent
{
    void inject(MainActivity mainActivity);

    @Subcomponent.Builder
    interface Builder
    {
        Builder mainActivityModule(MainModule module);

        MainComponent build();
    }
}

MainModule:

@Module
public class MainModule
{
    private MainActivity mainActivity;

    public MainModule(MainActivity mainActivity)
    {
        this.mainActivity = mainActivity;
    }

    @Provides
    public MainContract.View mainView()
    {
        return mainActivity;
    }

    @Provides
    public MainPresenter mainPresenter()
    {
        return new MainPresenter(mainActivity);
    }
}

AppComponent

@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent
{
    MainComponent.Builder mainActivityComponentBuilder();
}

应用:

public class App extends Application
{
    public static AppComponent appComponent;

    @Override
    public void onCreate()
    {
        super.onCreate();
        setupGraph();
    }

    private void setupGraph()
    {
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }

    @VisibleForTesting(otherwise = VisibleForTesting.NONE)
    public void setComponent(AppComponent component)
    {
        appComponent = component;
    }
}

样本运行良好。我错过了什么吗?

我已经上传了我在这里尝试的完整源代码:https://github.com/jbmlaird/DaggerMockRepository

1 个答案:

答案 0 :(得分:0)

此问题已在0.6.5版本中修复