Вопрос по android – Можно ли вводить макеты для тестирования в AndroidAnnotations?

8

Error: User Rate Limit Exceeded

@Bean(MyImplementation.class)
MyInterface myInterface;

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded apollodude217
Error: User Rate Limit Exceeded John Ericksen

Ваш Ответ

2   ответа
8

johncarl

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.

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
6

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

.

Error: User Rate Limit Exceeded apollodude217
Error: User Rate Limit Exceeded

Похожие вопросы