SlideShare una empresa de Scribd logo
1 de 11
Descargar para leer sin conexión
Introduction to
       AndroidMock

Li-Wei Cheng
Outline
•   What's Mock Object ?
•   Popular Mock libraries.
•   Why using AndroidMock ?
•   Example
•   Use AndroidMock in app
•   Use AndroidMock in system service
What's 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.
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++
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.
•   AndoirdMock's class mocking is achieved by
    o   Interface Mocking to EasyMock
    o   Use annotation to generate the helper class
Example
package 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
    }
}
Compilation flow for AndroidMock
Limitation for AndroidMock( EasyMock)
•   It can't mock final method.
    o   If it mocks, it will execute the origin logic.

•   It can't mock static method.
    o   If it mocks, it will execute the origin logic.

•   It can't mock private method.
    o   If it mocks, it will execute the origin logic.

•   some special methods can't be mocked
    o equals()
    o toString()
    o hashCode()
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 ?

Más contenido relacionado

Similar a Introduction to AndroidMock

Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworksTomáš Kypta
 
Mocking vtcc3 - en
Mocking   vtcc3 - enMocking   vtcc3 - en
Mocking vtcc3 - envgrondin
 
Make it test-driven with CDI!
Make it test-driven with CDI!Make it test-driven with CDI!
Make it test-driven with CDI!rafaelliu
 
How the HotSpot and Graal JVMs execute Java Code
How the HotSpot and Graal JVMs execute Java CodeHow the HotSpot and Graal JVMs execute Java Code
How the HotSpot and Graal JVMs execute Java CodeJim Gough
 
.NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012).NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012)David McCarter
 
Scott Mason: Enhancing the User Interface Using Titanium Modules
Scott Mason: Enhancing the User Interface Using Titanium ModulesScott Mason: Enhancing the User Interface Using Titanium Modules
Scott Mason: Enhancing the User Interface Using Titanium ModulesAxway Appcelerator
 
Testing in Android: automatici, di integrazione, TDD e scenari avanzati
Testing in Android: automatici, di integrazione, TDD e scenari avanzatiTesting in Android: automatici, di integrazione, TDD e scenari avanzati
Testing in Android: automatici, di integrazione, TDD e scenari avanzatiAlfredo Morresi
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android ApplicationsRody Middelkoop
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT studentsPartnered Health
 
Testcontainers - Geekout EE 2017 presentation
Testcontainers - Geekout EE 2017 presentationTestcontainers - Geekout EE 2017 presentation
Testcontainers - Geekout EE 2017 presentationRichard North
 
Refactor your way forward
Refactor your way forwardRefactor your way forward
Refactor your way forwardJorge Ortiz
 
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...Sigma Software
 
Genymotion with Jenkins
Genymotion with JenkinsGenymotion with Jenkins
Genymotion with JenkinsVishal Nayak
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock TutorialSbin m
 
Beginning iPhone Development
Beginning iPhone DevelopmentBeginning iPhone Development
Beginning iPhone Developmentsgleadow
 

Similar a Introduction to AndroidMock (20)

Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworks
 
Make it compatible
Make it compatibleMake it compatible
Make it compatible
 
Mocking vtcc3 - en
Mocking   vtcc3 - enMocking   vtcc3 - en
Mocking vtcc3 - en
 
Mock your way with Mockito
Mock your way with MockitoMock your way with Mockito
Mock your way with Mockito
 
Make it test-driven with CDI!
Make it test-driven with CDI!Make it test-driven with CDI!
Make it test-driven with CDI!
 
Robolectric Adventure
Robolectric AdventureRobolectric Adventure
Robolectric Adventure
 
How the HotSpot and Graal JVMs execute Java Code
How the HotSpot and Graal JVMs execute Java CodeHow the HotSpot and Graal JVMs execute Java Code
How the HotSpot and Graal JVMs execute Java Code
 
.NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012).NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012)
 
Scott Mason: Enhancing the User Interface Using Titanium Modules
Scott Mason: Enhancing the User Interface Using Titanium ModulesScott Mason: Enhancing the User Interface Using Titanium Modules
Scott Mason: Enhancing the User Interface Using Titanium Modules
 
Testing in Android: automatici, di integrazione, TDD e scenari avanzati
Testing in Android: automatici, di integrazione, TDD e scenari avanzatiTesting in Android: automatici, di integrazione, TDD e scenari avanzati
Testing in Android: automatici, di integrazione, TDD e scenari avanzati
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android Applications
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
 
Testcontainers - Geekout EE 2017 presentation
Testcontainers - Geekout EE 2017 presentationTestcontainers - Geekout EE 2017 presentation
Testcontainers - Geekout EE 2017 presentation
 
Mock with Mockito
Mock with MockitoMock with Mockito
Mock with Mockito
 
Refactor your way forward
Refactor your way forwardRefactor your way forward
Refactor your way forward
 
EasyMock for Java
EasyMock for JavaEasyMock for Java
EasyMock for Java
 
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
 
Genymotion with Jenkins
Genymotion with JenkinsGenymotion with Jenkins
Genymotion with Jenkins
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
 
Beginning iPhone Development
Beginning iPhone DevelopmentBeginning iPhone Development
Beginning iPhone Development
 

Más de Li-Wei Cheng

Unit testing in android
Unit testing in androidUnit testing in android
Unit testing in androidLi-Wei Cheng
 
Make Your SW Component Testable
Make Your SW Component TestableMake Your SW Component Testable
Make Your SW Component TestableLi-Wei Cheng
 
What's software testing
What's software testingWhat's software testing
What's software testingLi-Wei Cheng
 
OO design principle
OO design principleOO design principle
OO design principleLi-Wei Cheng
 

Más de Li-Wei Cheng (6)

Unit testing in android
Unit testing in androidUnit testing in android
Unit testing in android
 
Make Your SW Component Testable
Make Your SW Component TestableMake Your SW Component Testable
Make Your SW Component Testable
 
Test doubles
Test doublesTest doubles
Test doubles
 
JUnit
JUnitJUnit
JUnit
 
What's software testing
What's software testingWhat's software testing
What's software testing
 
OO design principle
OO design principleOO design principle
OO design principle
 

Último

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Último (20)

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

Introduction to AndroidMock

  • 1. Introduction to AndroidMock Li-Wei Cheng
  • 2. Outline • What's Mock Object ? • Popular Mock libraries. • Why using AndroidMock ? • Example • Use AndroidMock in app • Use AndroidMock in system service
  • 3. What's 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. 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. 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. • AndoirdMock's class mocking is achieved by o Interface Mocking to EasyMock o Use annotation to generate the helper class
  • 6. Example package 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. Compilation flow for AndroidMock
  • 8. Limitation for AndroidMock( EasyMock) • It can't mock final method. o If it mocks, it will execute the origin logic. • It can't mock static method. o If it mocks, it will execute the origin logic. • It can't mock private method. o If it mocks, it will execute the origin logic. • some special methods can't be mocked o equals() o toString() o hashCode()
  • 9. Reference • http://code.google.com/p/android-mock/ • http://en.wikipedia.org/wiki/Mock_object
  • 10. Appendix • Setup the Eclipse to use AndroidMock o Setup AndroidMock in the Eclipse.doc