Android部件使用Dagger 2进行测试 [英] Android Unit Tests with Dagger 2

查看:94
本文介绍了Android部件使用Dagger 2进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,使用Dagger 2进行依赖注入。我还使用最新的gradle构建工具,它允许构建变体进行单元测试,并使用一个用于仪器测试。我在我的应用程序中使用 java.util.Random ,我想模拟它进行测试。我正在测试的类不使用任何Android内容,所以它们只是常规的java类。

I have an Android app that uses Dagger 2 for dependency injection. I am also using the latest gradle build tools that allow a build variant for unit testing and one for instrumentation tests. I am using java.util.Random in my app, and I want to mock this for testing. The classes I'm testing don't use any Android stuff, so they're just regular java classes.

在我的主代码中我定义了一个组件在扩展 Application 类的类中,但在单元测试中我没有使用 Application 。我尝试定义测试模块组件,但Dagger不会生成组件。我还尝试使用我在应用程序中定义的 Component 并在构建时交换模块,但是应用程序的组件没有我的测试类的注入方法。如何为测试提供 Random 的模拟实现?

In my main code I define a Component in a class that extends the Application class, but in the unit tests I'm not using an Application. I tried defining a test Module and Component, but Dagger won't generate the Component. I have also tried using the Component that I defined in my application and swapping the Module when I build it, but the application's Component doesn't have inject methods for my test classes. How can I provide a mock implementation of Random for testing?

以下是一些示例代码:

应用程序:

public class PipeGameApplication extends Application {

    private PipeGame pipeGame;

    @Singleton
    @Component(modules = PipeGameModule.class)
    public interface PipeGame {
        void inject(BoardFragment boardFragment);
        void inject(ConveyorFragment conveyorFragment);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        pipeGame = DaggerPipeGameApplication_PipeGame.create();
    }

    public PipeGame component() {
        return pipeGame;
    }
}

模块:

@Module
public class PipeGameModule {

    @Provides
    @Singleton
    Random provideRandom() {
        return new Random();
    }
}

测试的基类:

public class BaseModelTest {

    PipeGameTest pipeGameTest;

    @Singleton
    @Component(modules = PipeGameTestModule.class)
    public interface PipeGameTest {
        void inject(BoardModelTest boardModelTest);
        void inject(ConveyorModelTest conveyorModelTest);
    }

    @Before
    public void setUp() {
        pipeGameTest = DaggerBaseModelTest_PipeGameTest.create(); // Doesn't work
    }

    public PipeGameTest component() {
        return pipeGameTest;
    }
}

或:

public class BaseModelTest {

    PipeGameApplication.PipeGame pipeGameTest;

    // This works if I make the test module extend
    // the prod module, but it can't inject my test classes
    @Before
    public void setUp() {
        pipeGameTest = DaggerPipeGameApplication_PipeGame.builder().pipeGameModule(new PipeGameModuleTest()).build();
    }

    public PipeGameApplication.PipeGame component() {
        return pipeGameTest;
    }
}

测试模块:

@Module
public class PipeGameTestModule {

    @Provides
    @Singleton
    Random provideRandom() {
        return mock(Random.class);
    }
}


推荐答案

这如果没有一些解决方法,Dagger 2(截至v.0.0.0)目前是不可能的。您可以在此处阅读相关内容。

This is currently impossible with Dagger 2 (as of v2.0.0) without some workarounds. You can read about it here.

有关可能的解决方法的更多信息:

More about possible workarounds:

使用Dagger2时创建测试依赖项

这篇关于Android部件使用Dagger 2进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆