Is it possible to introduce ridicule for testing using AndroidAnnotations?

I have not found examples of how to do this. I assume this is not possible based on such examples:

@Bean(MyImplementation.class) MyInterface myInterface; 

when the class for injection is already defined.

+8
android unit-testing ioc-container mocking android-annotations
source share
2 answers

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

If you are unit testing, I would suggest using mocks in the old-fashioned way, using setter and trying to test Java code without the involved attachment infrastructure. This will test your class in isolation and cost a lot more.

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 the activity of Android (and other Android extensions) is difficult due to the exception of throws and final classes / methods. This is where Robolectric comes in handy (and highly recommended) for creating / shadowing the Android API.

If you are integration testing, you may need a different approach. Personally, I will try not to scoff at integration tests, as I try to test the application in the way it will work during the production process. But, if you really want to taunt, you can use a similar approach to unit testing and introduce a layout after you step on the generated activity class. It is worth noting that you can perform integration tests directly on the hardware using frameworks such as Robotium .

More about your question, I don’t know about any AndroidAnnotations tools specifically for injecting Mocks or injecting Mocks into a nested application dependency tree.

+6
source share

Addition to johncarl :

  • It is impossible to tell AndroidAnnotations that you want to introduce mocks instead of real objects, because it works at compile time, so the code should always be ready for production.

  • I would recommend testing the generated actions in addition to Robolectric . Annotations add behavior to your code, so you should not test it as if there were no annotations.

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

  • You can allow the launch of AndroidAnnotations DI, and then re-modify the dependency. Fields have at least a default scope, which means that they can be accessed from the same package, so you will need to create a test in the same package as the action.

     MyActivity_ activity = new MyActivity_(); // myInterface gets injected activity.onCreate(null); // you reinject myInterface activity.myInterface = Mockito.mock(MyInterface.class); 
  • In AndroidAnnotations, dependencies are introduced by calling MyImplementation_.getInstance_() . You can use runtime byte code manipulation with a tool like PowerMock so that the getInstance_() MyImplementation_ returns the layout. This may require some initial work because you will need to mix the PowerMock test runner and the Robolectric test drive.

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

+8
source share

All Articles