tag:blogger.com,1999:blog-74425714818849821282024-02-08T22:24:14.035+05:30C.Karthik's Dev BlogSoftware EngineerKarthik Chandrarajhttp://www.blogger.com/profile/10274520141036718659noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-7442571481884982128.post-81207468439754800432013-04-25T10:15:00.003+05:302020-08-25T08:09:51.661+05:30Android unit testing with Robolectric - Simple examplesHere are some simple examples to learn unit testing android code using robolectric quickly.
<br />
<br />
<b>Robolectric examples :</b><br />
<ul>
<li><a href="http://ckarthik-tech.blogspot.com/2013/04/robolectric-testing-setcontentview.html">Testing setContentView</a></li>
</ul>
<ul>
<li><a href="http://ckarthik-tech.blogspot.com/2013/04/android-robolectric-unit-testing-start.html">Testing start of an activity</a></li>
</ul>
<ul>
<li><a href="http://ckarthik-tech.blogspot.com/2013/04/robolectric-test-sending-sms.html">Testing send SMS (SMS Manager)</a></li>
</ul>
<ul>
<li><a href="http://ckarthik-tech.blogspot.com/2013/04/android-robolectric-unit-testing.html">Testing request location updates (Location Manager)</a></li>
</ul>
Karthik Chandrarajhttp://www.blogger.com/profile/10274520141036718659noreply@blogger.com1tag:blogger.com,1999:blog-7442571481884982128.post-30951965642820113402013-04-24T23:01:00.001+05:302013-04-24T23:03:08.217+05:30Android - Robolectric - Unit testing request location updates (LocationManager)Here is the android code to register for network, GPS and passive location updates on activity create<br/><br/>
<strong>Android Code to test :</strong><br/>
<script src="https://gist.github.com/ckarthik17/7c3378f1efeb1b319647.js"></script><br/>
To test the above code,<br/>
1) First you need to create a mock location<br/>
2) Get the ShadowLocationManager using the robolectric's shadowOf method <br/>
3) This shadow Object has a method called 'simulateLocation' to mock a location update.<br/>
4) Now we can assert the actual value obtained by the listener with the expected value<br/><br/>
<strong>Robolectric Test :</strong>
<script src="https://gist.github.com/ckarthik17/a73ddcdca0d70a638599.js"></script><br />
Karthik Chandrarajhttp://www.blogger.com/profile/10274520141036718659noreply@blogger.com2tag:blogger.com,1999:blog-7442571481884982128.post-19403543385257539182013-04-20T21:53:00.000+05:302013-04-20T21:53:45.363+05:30Android - Robolectric - Unit testing start of an activityHere is the android code to start an activity on click of a button<br/><br/>
<strong>Android Code to test :</strong><br/>
<script src="https://gist.github.com/ckarthik17/ffaba23b71d64eaf3892.js"></script><br/>
To test the above code,<br/>
1) First, You can call button's performClick to simulate a click event <br/>
2) Now, Get the shadowActivity using the robolectric's shadowOf method <br/>
3) This shadow Object has all the methods to test an activity<br/>
4) We can get the next started activity's Intent, using the getNextStartedActivity() method and assert it<br/><br/>
<strong>Robolectric Test :</strong>
<script src="https://gist.github.com/ckarthik17/30c6510480e97a4e1754.js"></script><br />
Karthik Chandrarajhttp://www.blogger.com/profile/10274520141036718659noreply@blogger.com0tag:blogger.com,1999:blog-7442571481884982128.post-40609671609787246202013-04-15T23:13:00.000+05:302013-04-20T21:25:17.689+05:30Android - Robolectric- Unit testing send SMSSending SMS in android is so simple, you just have to get the default SMSManager and call the sendTextMessage<br/><br/>
<strong>Android Code :</strong><br/>
<script src="https://gist.github.com/ckarthik17/7ac20469286f19a63beb.js"></script><br/>
Testing the above code is even simpler,<br/>
1) Get the shadowSMSManager <br/>
2) From it, we can get the lastSentTextMessageParams, which has the details abt the last sent SMS.<br/>
3) Assert with the expected values<br/><br/>
<strong>Robolectric Test :</strong>
<script src="https://gist.github.com/ckarthik17/5d7c9352851cd54ad0ec.js"></script><br />Karthik Chandrarajhttp://www.blogger.com/profile/10274520141036718659noreply@blogger.com1tag:blogger.com,1999:blog-7442571481884982128.post-59802994576651679522013-04-14T15:54:00.001+05:302013-04-20T21:25:36.382+05:30Android - Robolectric - Unit testing setContentView<p>Normally the first thing we do in an Activity is to set the content view to a layout xml.<br>For example consider the below TestActivity, onCreate it sets its contentView to test_layout.xml </p><script src="https://gist.github.com/ckarthik17/ff2937e4481ebca5e77a.js"></script> <p>And the test_layout.xml is (<strong>Note : </strong>id of the root element <strong>android:id="@+id/test_layout_root"</strong>)<br><script src="https://gist.github.com/ckarthik17/991035ade43a02f98fc2.js"></script></p> <p>In the robolectric test, we need to get the activity's content view id and assert it with the layout id <script src="https://gist.github.com/ckarthik17/fd24228a67b0aaf32fe7.js"></script></p> Karthik Chandrarajhttp://www.blogger.com/profile/10274520141036718659noreply@blogger.com0tag:blogger.com,1999:blog-7442571481884982128.post-24374859659169886522013-04-14T11:55:00.001+05:302013-04-17T21:46:53.992+05:30Calabash Android - Testing start of an activity<p>Normally the first test you might write in calabash is to test the start of an activity.<br>For example consider that your app has a button(with text "Test"), and is configured to call 'launchTestActivity' onClick. </p> <p>Android code to start the activity : <br><script src="https://gist.github.com/ckarthik17/5438c5d7662f5d28de55.js"></script></p> <p>Now to test this, you need to write the below Calabash feature : <br><script src="https://gist.github.com/ckarthik17/3e576bebaa7cf18191d4.js"></script></p> Karthik Chandrarajhttp://www.blogger.com/profile/10274520141036718659noreply@blogger.com2