Is it possible to inject mocks for testing purposes with AndroidAnnotations?

孤街醉人 提交于 2019-12-09 16:07:27

问题


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.


回答1:


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

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.

What I mean:

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
    }
}

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.

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.

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.




回答2:


A complement to johncarl answer:

  • There's no way to tell AndroidAnnotations that you want to inject mocks instead of real objects, because it works at compile time, so the code must always be production ready.

  • I would recommend testing the generated activities, in complement with Robolectric. The annotations are adding behavior to your code, so you shouldn't test it as if there were no annotations.

  • Be careful of testing your activities behavior, not AndroidAnnotations' behavior. The framework already has tests of its own to check that the annotations work correctly :).

  • You can let AndroidAnnotations DI take place, and then reinject the mocked dependency. The fields have at least default scope, which mean they can be accessed from the same package, so you'd have to create the test in the same package as the activity.

    MyActivity_ activity = new MyActivity_();
    
    // myInterface gets injected 
    activity.onCreate(null);
    
    // you reinject myInterface
    activity.myInterface = Mockito.mock(MyInterface.class);
    
  • In AndroidAnnotations, dependencies are injected by calling MyImplementation_.getInstance_(). You could use runtime bytecode manipulation with a tool such as PowerMock to let the getInstance_() method of MyImplementation_ return a mock. This might require some initial work though, because you'd have to mix PowerMock test runner and Robolectric test runner.

Edit: I updated the documentation with content based on this question.



来源:https://stackoverflow.com/questions/10464375/is-it-possible-to-inject-mocks-for-testing-purposes-with-androidannotations

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