I am fairly new to unit testing, in android specifically. I have written an application with numerous activities. However, some of these activities rely on certain objects for them to populate the activity's views. I guess my question is: Is there a way I can mock up the objects within the Activity and make the activity use that created mock object from my Junit tests? Thanks in advance, I cant find the solution for this anywhere?
You can also take a look at AndroidMock:
http://code.google.com/p/android-mock/wiki/UsingAndroidMock
It is a Mock Objects framework for Android build on top of EasyMock.
If you refere to the developer guide under Create a Local Unit Test Class heading, you will find the answer to your question. Also refer to the "Mock Android Dependecies" sub heading on the same page.
Related
I want to run an Espresso instrumentation test for my application. Test contains navigation to multiple activities and interactions with several widgets.
What could be the best possible coding style I should follow to keep code clear and maintainable.
For now I had create different class for each activity and access static test method as follow:
#Test
public void validateUserNavigationScenario() {
// Create a bitmap we can use for our simulated camera image
SignOnActivity.validateLogin();
ProductSelector.selectProduct();
ProductDetail.showProductDetails();
ProductDetail.addProductToCart();
pressBack();
ProductSelector.selectProduct();
// ... additional test steps and validation ...
}
Coding test code is not different from coding production code.
The same good patterns and habits used for production code should be present in testing code. Using static reference as the main approach to structure all your suits seems to me a bad decision.
You should check it out Jake Wharton's talk about how to structure your testing code.
Instrumentation Testing Robots
It is focus on Kotlin development, but the same principles apply for Java. To sump up, it claims that you should hide your internal details inside some sort of “robot” pattern. Which means that you should try to be as much semantic as possible with your exposing API in order to create readable and maintainable tests.
Think on your test as if they were be used for other devs, and then think that you are one of them. How do you want it to use them?
I want to some feedback on why I should use mockito when testing my android application and also does anyone have any real examples of mockito would be extremely useful.
Such as a git project with various use cases of mocking android tests.
Mock objects are useful when you want to test part of an application when either the rest of the application isn't written yet, or when you don't want to actually have the side effects that the real code causes (such as writing to a database).
Mocking objects is commonly used it unit testing.
Unit tests are made to assure that the flow of instructions in your code is correct, and the logic is OK, but doing it separately from other classes/objects. Just one class at a time.
So, when your code makes use of any object of other class - you mock it and it behaves like correct object of mocked class, without actually making object of that class.
I suggest you reading about unit testing, injection, mockito.
Hope it helps you to get the idea behing mock.
I am trying to write functional tests for an android application. The problem is that most functional testing frameworks that I have explored (calabash-android, robotium) have a limitation on the number of activities from different applications that can be tested in the same test. So if in one workflow I need to select some contacts from the android contact picker I cant test that entire flow because the contact picker activity is part of the android contacts application and the framework can't test an activity from my application and the contacts application at the same time.
One possible solution my team thought of was to mock out the call to the contacts activity to return a dummy intent with contact information so that our application workflow can be tested. We are trying to use mockito to achieve this. However I am stuck pretty early on. Here is what I am trying to do
MyActivity mockActivity = mock(MyActivity.class);
when(mockActivity.startActivityForResult(<?>,anyInt())).thenReturn(fakeIntent);
I am not sure what to put in the first parameter in the second line. I have tried Intent.class and android.content.Intent.class however it throws a compile error. If anyone has worked with mocking activities using mockito, some help will be greatly appreciated.
P.S. - If I understand correctly mocking is used more in unit testing than functional testing. So these tests would be more of a hybrid. If anyone has a better suggestion on how to go about these functional tests on android I am all ears.
It's hard to answer this without knowing the signature of your startActivityForResult method, but the general idea is to use any(Xxx.class), where Xxx is the type of the parameter. So either
when(mockActivity.startActivityForResult(any(Xxx.class),anyInt())).thenReturn(fakeIntent);
or the (kind of equivalent)
doReturn(fakeIntent).when(mockActivity).startActivityForResult(any(Xxx.class),anyInt());
The issue is that you cannot really "mock" (actually "spy") on the activity you're testing since it is created out of your control by Android's instrumentation code. In a unit-test environment where you would have control, you could follow the mock(MyActivity.class) or spy(myActivityInstance) path (spy would actually be better because you could re-use most of the activity's original implementation), but here not.
The only solution I found for this dilemma was to move certain functionality out of the activity into utility classes, ideally utilizing roboguice for that (#ContextSingletons can be used to process activity results). Then, in your test project, you would create your own test guice injector, set that as base application injector before you call getActivity() for the first time and then let the activity work on your mocked utility class.
I outlined the complete process here.
I have a series of Java classes which have programmatic UI layouts; the structure of the layouts varies throughout the running of the classes and is dependent on user responses so I cannot create them as fixed layouts in an xml file. I am now at the stage that I need to test the classes and am wrestling with the Android ans Eclipse testing using JUnit. I cannot seem to workout how to test these programmatic UI's as their id's are assigned when they are created. I know what the id's are (I assign the id using setId(100 +i) where i is the loop variable) but cannot access these id's in the test project. I have tried findViewById(SandS.Med.NurseCalc.101) but it does not seem to work. I am trying this in my setUp() module.
Can anyone either help with this item or point me in the direction of a site that looks at testing in a more practical way rather than the rather theoretical and complex way in the android.developer site.
Thanks in anticipation.
I find mocking extremely usefull for testing programatic android stuff. I recommend jMockit
for this purpose ( IMHO, best mocking framework around )
In this unit test I test specific Vew retrieval:
https://github.com/ko5tik/andject/blob/master/src/test/java/de/pribluda/android/andject/ViewInjectionTest.java
I'm developing an Android application with database storage.
I want to test some classes that I use to access database.
I've found ActivityInstrumentationTestCase2 to test Activities but I haven't found anything to test classes.
How can I do that? I haven't used jUnit before.
Thanks.
I always use AndroidTestCases when writing unit tests for the Android platform. They provide access to a Context instance (if required), but otherwise work like the standard JUnit test class. You may also need to look at AndroidTestRunner to test your classes. There are some good tutorials out there; now that you know what to look for, I'm sure you can find them. Happy hunting! :D