Why should i mock objects? - android

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.

Related

Check whether a line is present in a class using Mockito

I am currently studying about Junit test cases, Mockito, Power Mockito and reflection. I am stuck somewhere in between, I want to check in a class whether a statement exits in that class or not using mockito. I have no idea that can I do that or not. I want to search "AndroidSupportInjection.inject(this)" line in that class. If anybody has idea how to do that. Please let me know.
I assume you are trying to search for that line in order to override the Dagger 2 request for injection and inject your own test doubles.
This is probably the wrong way to do things.
The best way to perform automated UI tests against Activities and Fragments is to isolate the depenedencies you want inside Dagger 2 modules and then use build variants to swap these with test doubles.
There is an example of this in the Google Android Architecture Blueprints
If this doesn't work, you could try using a setter on the injector like in DaggerMock

Unit testing using Eclipse - JUNIT

I wrote an android application that uses GPS mainly, using Eclipse.
I want to do a JUNIT test for the application.
What is the best way for specifying multiple values for my unit tests?
In each Test you should use one of the many available asserts http://junit.org/apidocs/org/junit/Assert.html to check whether the value returned by the tested method is the correct one. while testing the method you can pass any parameters you need to it.
for instance
assertEquals("OK",myObj.methodUnderTest(param1,param2,param3));
Maybe I misunderstood the question, but it sounds like you want parameterized tests. This allows you to write one test but test several different values against that one test.
I'm more familiar with NUnit's parameterized unit tests, but here's a link to JUnit's documentation on parameterized unit tests.

Android JUnit and parsing xml data - what to test

I am trying to get more experience on JUnit and its usage in Android.
Referring to this official Android training Parsing XML data I wonder if anyone can provide with an example on how to test some of the used methods.
Particularly how would you test the methods included in the class StackOverflowXmlParser and the methods loadXmlFromNetwork() and downloadUrl() [class NetworkActivity.java]
The best advice I can give you on unit testing is to first really understand what a unit of test is. When I write tests, in particular Unit Tests, I make sure my unit of test is a single class. EVERYTHING ELSE is mocked, and my test makes sure every public method on the class does what it promises.
That said, the methods you are asking about are EVIL UNTESTABLE CODE. It's a bit shocking to see code like this come from a Google engineer. My glancing guess is it's a front end web developer because the variables are all declared at the top of the method, Javascript style and initialization of every local variable that doesn't have a value to null suggests whoever wrote the example isn't very experienced with Java.
You would have to significantly refactor the methods to get them into a testable state. For instance loadXmlFromNetwork presents an API that lies. It isn't "loading" xml from the network, it's also parsing it into a List<Entry> then after that is done, it does more by cramming data from these entries into an HTML formatted String and then returns that.
The first problem with just this method alone is that it's creating objects inside of itself, instead of asking for what it needs. This presents a problem for testing because you can't mock these objects to test the logic. In a test you wouldn't want to have to make a network dependency, so you'd want to mock the HttpURLConnection and mock the behavior to exercise YOUR code.
To point you in the right direction, watch this video from Google's lead testing evangelist:
http://www.youtube.com/watch?v=wEhu57pih5w

Robotium with Mockito or Easy Mock

I'm new to Android testing and I`d really appreciate if some of you could help me with that.
I'm using robotium as automation testing framework (so far so good), but I have no idea how I use mockito or Easy Mock to add some mocks to my tests. I'm really stuck with that. Can someone give me some simple example on how to achieve this?
Thanks in advance
Short Answer/opinion
I don't recommend using Mockito for Android unit testing. The Android environment feels too complex to mock. The basic approach for Android unit tests is to run them within the emulator. Thus many of the container classes are already present and need not be mocked.
Long Answer
I also am relatively new to the world of Android unit testing. I have long written server-side unit tests and found Mockito to be one of the best tools around to simplify unit testing. Mockito is very helpful in mocking the behavior of complex objects. This helps to break any dependencies that your code-under-test may have on containers (e.g. servlet container or OSGI container), or on other complex collaborators (e.g. database connection classes).
This sort of mocking works well when your containers/collaborators have well-defined interfaces.
A couple months back, I decided to try Mockito with Android development. I found that Mockito does work if you have at least 1.9.5 and dexmaker. Dexmaker handles runtime byte-code generation for Android's Dalvik VM.
The first thing I tried in my very first test was to mock android.content.Context, and I found that is was HARD. First I tried to mock only the methods that I called directly, but then I found that these called into other methods which had dependencies on application resources, ... Eventually the mocking got so complicated that it defeated the purpose of using Mockito in the first place.
So I gave in and started using the Android unit test helper classes (AndroidTestCase, ActivityUnitTestCase, ...). I was discouraged that I now had to rely on the emulator which meant SLOW test execution. Perhaps Mockito still has a place in this type of test. It might be useful for mocking things like external data sources.
Anyways, this is just my 2 cents.

jUnit testing Database operations

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

Categories

Resources