unit testing android applications
TRANSCRIPT
![Page 1: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/1.jpg)
Tests, Stubs, Mocks
Effective Unittesting for android
![Page 2: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/2.jpg)
Goals of test automation
Improve quality Understand the SUT Reduce the risk Easy to run Easy to write Easy to maintain
SubjectUnderTest
![Page 3: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/3.jpg)
Principles in test automation
Write the test first. Each test should be:
– Small and simple– Independent to other test– Repeatable– Self-checking Fully Automated
First do “State verification” and then “Behavior Verification”.
![Page 4: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/4.jpg)
Which part can be automated?
![Page 5: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/5.jpg)
System Under Test
It may have Depended-on components
Image From: xunit Test Patterns, G. Meszaros
![Page 6: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/6.jpg)
What are we talking about?
Pattern: Test Double Also known as: Imposter Problem: How can we verify logic
independently when code it depends on is unusable? How can we avoid Slow Tests?
Solution: We replace a component on which the SUT depends with a "test-specific equivalent."
![Page 7: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/7.jpg)
WHAT?
Test Doubles: Dummy Object: Temporary Object that does not influence the SUT Test Stub: Hand coded object used for testing Test Spy: Verification occurs after the test method has been called Mock Object: Expectations configured before calling tests Fake Object: Typically, it implements the same functionality as the real
DOC but in a much simpler way, no expectation are configured.
![Page 8: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/8.jpg)
Mock? Why? Mock - a simulated object that
mimics the behavior of a real object in controlled ways.
![Page 9: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/9.jpg)
Four phase testing
Image From: xunit Test Patterns, G. Meszaros
![Page 10: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/10.jpg)
Test structureSetup
Execute
Verify
![Page 11: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/11.jpg)
Effective test automation
After test generation by considering all paths and the features and organization specified, our test still may have these bad smells:– Slow Tests– Test Code Duplication– Obscure Tests– Buggy Tests
![Page 12: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/12.jpg)
Image SRC: www.dilbert.com
![Page 13: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/13.jpg)
Mockito, how to drink it? framework basics
import static org.mockito.Mockito.*; //mock creationLinkedList mockedList = mock(LinkedList.class);
//using mock objectmockedList.add("one");mockedList.clear();
//verificationverify(mockedList).add("one");verify(mockedList).clear();
Stub - Mockito can mock concrete classes, not only interfaces
![Page 14: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/14.jpg)
Mockito, how to drink it? framework basics
Return value thenReturn()when(mock.someMethod("some arg")).thenReturn("foo");
Stubbing voids requires doReturn() doReturn("bar").when(mock).foo();
![Page 15: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/15.jpg)
What else to use
PowerMock (private, final,static methods)
Jmockit (constructors and static methods mocking)
Hamcrest (library of matcher objects (also known as constraints or predicates) allowing 'match' rules to be defined declaratively)
![Page 16: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/16.jpg)
ANDROID UNIT TESTING
![Page 17: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/17.jpg)
EXAMPLE: JUNIT + ROBOLECTRIC for Android
Android unit testing is tricky:– android.jar only contains mocked out .class
files which leads to java.lang.RuntimeException: Stub!
TIPS:– Keep things simple by trying to make as many
services as possible not dependent on the parts of the Android platform that are not compatible with a conventional JVM.
– Robolectric to the rescue
![Page 18: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/18.jpg)
Unit test depending on Android api
@RunWith(RobolectricTestRunner.class)public class PopularRoutesAdapterTest { private PopularRoutesAdapter adapter;
@Before public void setUp() { adapter = new PopularRoutesAdapter(
Robolectric.buildActivity(Activity.class).create().get(); }
@Test public void testAddItem() { assertEquals(0, adapter.getCount()); adapter.addItem(new Route("Arnhem")); assertEquals(1, adapter.getCount()); }
Special test runner coming with Robolectric
Activity class is mocked by Robolectric
![Page 19: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/19.jpg)
General rules to remember
Mock it outside your code If you cannot test your code -> then probably
you should change it ;) cause its badly written
Test first Only one concrete class , mock the rest Only mock your neirest neighbour (Law of
Demeter -> dont talk with strangers) Think ;) and then write
![Page 20: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/20.jpg)
Robo-WTFRobolectric
Has Java implementations of class files for most of the Android API
Enables true unittesting, being not dependent on any network, hardware, device or database
Roboguice
Guice for RobolectricDependency Injection
based in Google GuiceCode bases DI instead
of XML (like in Spring)
![Page 21: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/21.jpg)
Deckard
Combination of – Gradle / Maven– Robolectric– Junit
Example adds– Roboguice– Mockito
![Page 22: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/22.jpg)
DEMO
![Page 23: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/23.jpg)
Developers Not Writing Tests
Symptoms:– No tests can be found when you ask to see the
• unit tests for a task,• customer tests for a User Story,
– Lack of clarity about what a user story or task really means Impact:
– Lack of safety net– Lack of focus
Possible Causes:– Hard to Test Code?– Not enough time?– Don’t have the skills?– Have been told not to?– Don’t see the value?
Robolectric for unittesting
Espresso for scenario-testing
![Page 24: Unit Testing Android Applications](https://reader034.vdocument.in/reader034/viewer/2022042706/58a5f7391a28aba5728b6bb1/html5/thumbnails/24.jpg)
Resources
https://github.com/ddoa/dea-code-examples/android-deckard-unittest/
http://pages.cpsc.ucalgary.ca/~maurer/uploads/SENG515615F2007/XUnitTestPattern.ppt
http://code.google.com/p/mockito/ https://docs.google.com/presentation/d/1J0W
iFJI9wSkc3UMsNXYv8ixX5orjkYzduwyOalzEkjU/embed?hl=pl&size=l#slide=id.p5