是否有可能注入嘲笑为测试目的Andr​​oidAnnotations? [英] Is it possible to inject mocks for testing purposes with AndroidAnnotations?

查看:138
本文介绍了是否有可能注入嘲笑为测试目的Andr​​oidAnnotations?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到如何做到这一点任何的例子。我假设它基于这样的例子是不可能的:

I haven't found any examples on how to do this. I'm assuming it is not possible based on examples like this:

@Bean(MyImplementation.class)
MyInterface myInterface;

在已确定的类别来注入。

where the class to inject is already determined.

推荐答案

现在的问题是,你的单元测试和集成测试?

The question is, are you unit testing or integration testing?

如果你是单元测试,我建议使用嘲弄的老式方法,通过使用一个二传手,并试图测试Java code,而不涉及的依赖注入框架。这将考验你的类隔离和回避了很多复杂性。

If you are unit testing, I would suggest using mocks the old fashioned way, by using a setter and trying to test the Java code without the dependency injection framework involved. This will test your class in isolation and sidesteps a lot of complexity.

我的意思:

public class Test{

    ClassInTest inTest;
    MyInterface myInterface;

    @Before
    public void setup(){
         inTest = new ClassInTest();
         //or your favorite mocking frameowrk
         myInterface = EasyMock.createMock(MyInterface.class);  
         inTest.setMyInterface(myInterface);
    }

    @Test
    public void testMethod(){
        //...mocking test code
    }
}

当然,测试Android的活动(和Android的其他扩展名)是因为异常投掷存根和最终的类/方法的困难。这是 Robolectric 就派上用场了(并极力推荐)实例化/遮蔽了Android API。

Of course, testing Android Activities (and other extensions of Android) is difficult because of the exception throwing stubs and final classes/methods. This is where Robolectric comes in handy (and highly recommended) for instantiating/shadowing the Android API.

如果你是集成测试,你可能需要采取另一种方法。就个人而言,我会尽量不要在集成测试嘲笑,因为我尝试测试的应用程序,它会在生产中运行。但是,如果你真的想嘲笑,你可以使用类似的方法,以单元测试和介绍一个模拟你站起来,你生成的活动下课。值得注意的,你可以直接使用类似框架的硬件进行集成测试 Robotium

If you are integration testing you may want to take another approach. Personally, I would try not to mock during integration tests as I try to test the application as it would run in production. But, if you really want to mock, you could use a similar approach to unit testing and introduce a mock after you stand up your generated Activity class. Worth noting, you can perform integration tests directly on the hardware using frameworks like Robotium.

更多你的问题,我不知道AndroidAnnotations的任何设施,专门用于注射嘲弄和嘲笑引入到应用程序中注入依赖关系树。

More to your question, I am not aware of any facilities of AndroidAnnotations specifically for injecting Mocks or introducing Mocks into the injected dependency tree of an application.

这篇关于是否有可能注入嘲笑为测试目的Andr​​oidAnnotations?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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