Introduction to       AndroidMockLi-Wei Cheng
Outline•   Whats Mock Object ?•   Popular Mock libraries.•   Why using AndroidMock ?•   Example•   Use AndroidMock in app•...
Whats Mock Object ?•   A simulated objects that mimic the behavior of    real objects in controlled ways.•   Usually used ...
Popular mock libraries•   EasyMock    o   Java    o   EasyMock 3.1 has the feature for Class Mocking•   JMock    o   Java ...
Why using AndroidMock ?•   Dalvik VM is limited for supporting mocking.•   EasyMock only can support interface mocking    ...
Examplepackage com.google.android.testing.mocktest.test;import com.google.android.testing.mocking.AndroidMock;public class...
Compilation flow for AndroidMock
Limitation for AndroidMock( EasyMock)•   It cant mock final method.    o   If it mocks, it will execute the origin logic.•...
Reference•   http://code.google.com/p/android-mock/•   http://en.wikipedia.org/wiki/Mock_object
Appendix•   Setup the Eclipse to use AndroidMock    o   Setup AndroidMock in the Eclipse.doc
Any Question ?
Upcoming SlideShare
Loading in...5
×

Introduction to AndroidMock

1,007

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,007
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to AndroidMock

  1. 1. Introduction to AndroidMockLi-Wei Cheng
  2. 2. Outline• Whats Mock Object ?• Popular Mock libraries.• Why using AndroidMock ?• Example• Use AndroidMock in app• Use AndroidMock in system service
  3. 3. Whats Mock Object ?• A simulated objects that mimic the behavior of real objects in controlled ways.• Usually used in Unit Test Framework. o Used to do the isolation test.• Java has many popular mock libraries such as EasyMock, JMock ... etc.• Android also has its mock library called AndroidMock.
  4. 4. Popular mock libraries• EasyMock o Java o EasyMock 3.1 has the feature for Class Mocking• JMock o Java Mock Library• moQ o .NET Interface Class +• Google Mock Mocking Mocking o C++
  5. 5. Why using AndroidMock ?• Dalvik VM is limited for supporting mocking.• EasyMock only can support interface mocking in Dalvik VM even it has realized the class mocking.• AndroidMock is based on EasyMock.• AndoirdMocks class mocking is achieved by o Interface Mocking to EasyMock o Use annotation to generate the helper class
  6. 6. Examplepackage com.google.android.testing.mocktest.test;import com.google.android.testing.mocking.AndroidMock;public class MockingTest extends TestCase { @UsesMocks(ClassToMock.class) 1. pre-generate the helper class for AndroidMock public void testMocks() throws ClassNotFoundException { ClassToMock myMockObject = 2. create a mock object AndroidMock.createMock(ClassToMock.class); AndroidMock.expect(myMockObject.getString()).andReturn("Woohoo"); AndroidMock.expect(myMockObject.getNextInt(2)).andReturn(42); 3. record behavior AndroidMock.replay(myMockObject); 4. replay the behavior assertEquals("Woohoo", myMockObject.getString()); assertEquals(42, myMockObject.getNextInt(2)); 5. test AndroidMock.verify(myMockObject); 6. verified whether all the record behaviors are executed }}
  7. 7. Compilation flow for AndroidMock
  8. 8. Limitation for AndroidMock( EasyMock)• It cant mock final method. o If it mocks, it will execute the origin logic.• It cant mock static method. o If it mocks, it will execute the origin logic.• It cant mock private method. o If it mocks, it will execute the origin logic.• some special methods cant be mocked o equals() o toString() o hashCode()
  9. 9. Reference• http://code.google.com/p/android-mock/• http://en.wikipedia.org/wiki/Mock_object
  10. 10. Appendix• Setup the Eclipse to use AndroidMock o Setup AndroidMock in the Eclipse.doc
  11. 11. Any Question ?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×