SlideShare a Scribd company logo
Unit and
Automation
Testing
on Android
Stanislav Gatsev
Android Team Lead,
Melon Inc.
Who am I?
Stanislav Gatsev
Android Team Lead
+359 89 891 4481
stanislav.gatsev@melontech.com
http://www.melon.bg
What is Android?
o World's most popular mobile
platform
o Every day another million users
power up their Android devices for
the first time
o Using Android SDK we can develop
software for almost everything from
smartphones and tablets to
glasses, watches, TVs and even
cars.
Why we automate testing?
o We save time !!!
o We improve our code quality
o We support live documentation
o We run our regression tests
o We fight fragmentation
Our Example Project
What Can We Test?
o We can test our logic, algorithms, calculations
o We can test our UI and navigation logic
o We can test IO operations, database and network
operations
o And more...
@SmallTest
public void test_addOperatorInJava() throws Exception {
Assert.assertEquals(4, 2 + 2);
Assert.assertEquals(27, 25 + 2);
}
@SmallTest
public void test_FieldsAreVisible() {
ViewAsserts.assertOnScreen(mRootView, mEmailEditText);
ViewAsserts.assertOnScreen(mRootView, mPasswordEditText);
ViewAsserts.assertOnScreen(mRootView, mLoginActionLayout);
}
How we automate testing?
o We use JUnit
o We mock our objects
o We are using activity testing framework
o We click with Robotium
JUnit and Android
We can use the JUnit TestCase class to do unit testing on a class that
doesn't call Android APIs. TestCase is also the base class for
AndroidTestCase, which we can use to test Android-dependent objects.
Besides providing the JUnit framework, AndroidTestCase offers
Android-specific setup, teardown, and helper methods.
public class UtilTests extends AndroidTestCase {
@SmallTest
public void test_StringToLatLngValidValues() throws Exception {
String latLngString = "34.009555,-118.497072";
LatLng latLng = Util.stringToLatLng(latLngString);
Assert.assertEquals(Double.compare(latLng.latitude, 34.009555), 0);
Assert.assertEquals(Double.compare(latLng.longitude, -118.497072), 0);
}
@SmallTest
public void test_StringToLatLngValidInvalues() throws Exception {
String latLngString = "34.009555,-a118.497072";
LatLng latLng = Util.stringToLatLng(latLngString);
Assert.assertEquals(Double.compare(latLng.latitude, 0), 0);
Assert.assertEquals(Double.compare(latLng.longitude, 0), 0);
}
}
Why we use Mocks?
The objective of unit testing is to exercise just one method at a time, but what
happens when that method depends on other things—hard-to-control things
such as the network, or a database.
The solution is the Mock object. It is simply a debug replacement for a real-
world object.
Mockito
Mockito is the way we mock objects in Android. As its developers say:
“Mockito is a mocking framework that tastes really good. It lets you write
beautiful tests with clean & simple API. Mockito doesn't give you hangover
because the tests are very readable and they produce clean verification
errors.”
• https://code.google.com/p/dexmaker/
• System property hack
Mockito and Android
//create Mock of server communication
final WeatherServerRequest weatherServerRequest =
Mockito.mock(WeatherServerRequest.class);
//stub the actual network call and return empty object
ServerResponse serverResponse = new ServerResponse();
Mockito.when(weatherServerRequest.getResponse()).thenReturn(serverResponse);
//create mock of the refresh-able and the observers subject
IRefreshable refreshable = Mockito.mock(IRefreshable.class);
ISubject subject = Mockito.mock(ISubject.class);
runWeatherWorkerThread(refreshable, subject, weatherServerRequest);
//verify if refresh-able start callback is called
Mockito.verify(refreshable).onStartRefresh();
//verify if the observers callback is called
Mockito.verify(subject).updateData(Mockito.anyMapOf(String.class,
ServerResponse.class));
//verify if refresh-able end callback is called
Mockito.verify(refreshable).onEndRefresh();
=+
Activity tests
ActivityInstrumentationTestCase2
This is the class which helps us test our Activities. It has direct reference to the
tested activity and you have the ability to run whole test or just parts of it in the
UI Thread.
@SmallTest
public void test_viewPagerHasAllLocations() throws Throwable {
//Creates the data fetcher mock and injects it to the Activity for every test
createDataFetcherMock();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
//reinitialize activity with new data fetcher
getActivity().init();
}
});
//asserts if the view pager has the same number of items the data fetcher returned
Assert.assertEquals(3, mViewPager.getAdapter().getCount());
}
Robotium
o It is a lot easier to write our automation tests
o Helpful API for executing user actions
o Flexible results assertion
o Hybrid apps are supported
o We can use it in our Activity tests
Robotium in action
@LargeTest
public void test_addNewLocation() throws Throwable {
getInstrumentation().waitForIdleSync();
moveMapToPosition();
getInstrumentation().waitForIdleSync();
//gets the root view of the Activity
View rootView = getActivity().findViewById(android.R.id.content);
//long click in the center of the screen
mSolo.clickLongOnScreen(rootView.getWidth()/2, rootView.getHeight()/2);
getInstrumentation().waitForIdleSync();
//get first weather location
WeatherDataFetcher dataFetcher = getActivity().getDataFetcher();
List<WeatherLocation> weatherLocations = dataFetcher.getLocationsList();
Assert.assertEquals(1, weatherLocations.size());
//check if the location is the right one
WeatherLocation weatherLocation = weatherLocations.get(0);
Assert.assertEquals("Santa Monica, CA", weatherLocation.getLocationName());
//clears DB
dataFetcher.removeLocation(0);
}
Thank you!
Questions?
References
o https://developer.android.com
o https://code.google.com/p/robotium/
o https://github.com/mockito/mockito
o http://blog.gfader.com/2010/10/why-are-automated-tests-so-important.html
o http://media.pragprog.com/titles/utj/mockobjects.pdf
o http://www.embedded.com/design/prototyping-and-
development/4398723/The-mock-object-approach-to-test-driven-
development

More Related Content

What's hot

Mockito intro
Mockito introMockito intro
Mockito intro
Jonathan Holloway
 
Stop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScriptStop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScript
Ryan Anklam
 
Unit testing
Unit testingUnit testing
Junit 5 - Maior e melhor
Junit 5 - Maior e melhorJunit 5 - Maior e melhor
Junit 5 - Maior e melhor
Tiago de Freitas Lima
 
Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
Sunil kumar Mohanty
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Anup Singh
 
Junit mockito and PowerMock in Java
Junit mockito and  PowerMock in JavaJunit mockito and  PowerMock in Java
Junit mockito and PowerMock in Java
Ankur Maheshwari
 
1 aleksandr gritsevski - attd example using
1   aleksandr gritsevski - attd example using1   aleksandr gritsevski - attd example using
1 aleksandr gritsevski - attd example using
Ievgenii Katsan
 
JUnit 5
JUnit 5JUnit 5
JMockit
JMockitJMockit
JMockit
Angad Rajput
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
Yaron Karni
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
Abner Chih Yi Huang
 
Java Quiz - Meetup
Java Quiz - MeetupJava Quiz - Meetup
Java Quiz - Meetup
CodeOps Technologies LLP
 
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, howTomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
Марія Русин
 
Thread & concurrancy
Thread & concurrancyThread & concurrancy
Thread & concurrancy
Onkar Deshpande
 

What's hot (20)

Mockito intro
Mockito introMockito intro
Mockito intro
 
Stop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScriptStop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScript
 
Unit testing
Unit testingUnit testing
Unit testing
 
Junit 5 - Maior e melhor
Junit 5 - Maior e melhorJunit 5 - Maior e melhor
Junit 5 - Maior e melhor
 
Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
 
Mockito
MockitoMockito
Mockito
 
Junit mockito and PowerMock in Java
Junit mockito and  PowerMock in JavaJunit mockito and  PowerMock in Java
Junit mockito and PowerMock in Java
 
1 aleksandr gritsevski - attd example using
1   aleksandr gritsevski - attd example using1   aleksandr gritsevski - attd example using
1 aleksandr gritsevski - attd example using
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
JMockit
JMockitJMockit
JMockit
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
Java Quiz - Meetup
Java Quiz - MeetupJava Quiz - Meetup
Java Quiz - Meetup
 
Unit testing with java
Unit testing with javaUnit testing with java
Unit testing with java
 
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, howTomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
 
3 j unit
3 j unit3 j unit
3 j unit
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
Thread & concurrancy
Thread & concurrancyThread & concurrancy
Thread & concurrancy
 

Similar to Unit & Automation Testing in Android - Stanislav Gatsev, Melon

Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
pleeps
 
Agile mobile
Agile mobileAgile mobile
Agile mobile
Godfrey Nolan
 
Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015
Buşra Deniz, CSM
 
Hitchhiker's guide to Functional Testing
Hitchhiker's guide to Functional TestingHitchhiker's guide to Functional Testing
Hitchhiker's guide to Functional Testing
Wiebe Elsinga
 
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
Tomáš Kypta
 
Testing in android
Testing in androidTesting in android
Testing in android
jtrindade
 
Android testing
Android testingAndroid testing
Android testing
Sean Tsai
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
javatwo2011
 
Alexey Buzdin "Maslow's Pyramid of Android Testing"
Alexey Buzdin "Maslow's Pyramid of Android Testing"Alexey Buzdin "Maslow's Pyramid of Android Testing"
Alexey Buzdin "Maslow's Pyramid of Android Testing"
IT Event
 
Thomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalThomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalDroidcon Berlin
 
The real beginner's guide to android testing
The real beginner's guide to android testingThe real beginner's guide to android testing
The real beginner's guide to android testing
Eric (Trung Dung) Nguyen
 
Google mock training
Google mock trainingGoogle mock training
Google mock training
Thierry Gayet
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockRobot Media
 
Automating Tactically vs Strategically SauceCon 2020
Automating Tactically vs Strategically SauceCon 2020Automating Tactically vs Strategically SauceCon 2020
Automating Tactically vs Strategically SauceCon 2020
Alan Richardson
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
Sanjib Dhar
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
Testing Java Microservices Devoxx be 2017
Testing Java Microservices Devoxx be 2017Testing Java Microservices Devoxx be 2017
Testing Java Microservices Devoxx be 2017
Alex Soto
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
Dev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_testDev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_test
Masanori Kato
 
Java performance
Java performanceJava performance
Java performance
Sergey D
 

Similar to Unit & Automation Testing in Android - Stanislav Gatsev, Melon (20)

Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Agile mobile
Agile mobileAgile mobile
Agile mobile
 
Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015
 
Hitchhiker's guide to Functional Testing
Hitchhiker's guide to Functional TestingHitchhiker's guide to Functional Testing
Hitchhiker's guide to Functional Testing
 
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
 
Testing in android
Testing in androidTesting in android
Testing in android
 
Android testing
Android testingAndroid testing
Android testing
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
Alexey Buzdin "Maslow's Pyramid of Android Testing"
Alexey Buzdin "Maslow's Pyramid of Android Testing"Alexey Buzdin "Maslow's Pyramid of Android Testing"
Alexey Buzdin "Maslow's Pyramid of Android Testing"
 
Thomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalThomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-final
 
The real beginner's guide to android testing
The real beginner's guide to android testingThe real beginner's guide to android testing
The real beginner's guide to android testing
 
Google mock training
Google mock trainingGoogle mock training
Google mock training
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
 
Automating Tactically vs Strategically SauceCon 2020
Automating Tactically vs Strategically SauceCon 2020Automating Tactically vs Strategically SauceCon 2020
Automating Tactically vs Strategically SauceCon 2020
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Testing Java Microservices Devoxx be 2017
Testing Java Microservices Devoxx be 2017Testing Java Microservices Devoxx be 2017
Testing Java Microservices Devoxx be 2017
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Dev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_testDev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_test
 
Java performance
Java performanceJava performance
Java performance
 

More from beITconference

ADASTRA +1 or How YOU generate additional value in the project lifecycle - Пе...
ADASTRA +1 or How YOU generate additional value in the project lifecycle - Пе...ADASTRA +1 or How YOU generate additional value in the project lifecycle - Пе...
ADASTRA +1 or How YOU generate additional value in the project lifecycle - Пе...beITconference
 
NoSQL and Cloud Services - Philip Balinow, Comfo
NoSQL and Cloud Services -  Philip Balinow, ComfoNoSQL and Cloud Services -  Philip Balinow, Comfo
NoSQL and Cloud Services - Philip Balinow, ComfobeITconference
 
Mobile First with Angular.JS - Владимир Цветков, Obecto
Mobile First with Angular.JS - Владимир Цветков, ObectoMobile First with Angular.JS - Владимир Цветков, Obecto
Mobile First with Angular.JS - Владимир Цветков, ObectobeITconference
 
Уроците от работата ми по WordPress.com - Веселин Николов
Уроците от работата ми по WordPress.com - Веселин НиколовУроците от работата ми по WordPress.com - Веселин Николов
Уроците от работата ми по WordPress.com - Веселин НиколовbeITconference
 
Scrum Crash Course - Anatoli Iliev and Lyubomir Cholakov, Infragistics
Scrum Crash Course - Anatoli Iliev and Lyubomir Cholakov, InfragisticsScrum Crash Course - Anatoli Iliev and Lyubomir Cholakov, Infragistics
Scrum Crash Course - Anatoli Iliev and Lyubomir Cholakov, InfragisticsbeITconference
 
Развитие на финансовите приложения от транзакционни услуги към комплексно реш...
Развитие на финансовите приложения от транзакционни услуги към комплексно реш...Развитие на финансовите приложения от транзакционни услуги към комплексно реш...
Развитие на финансовите приложения от транзакционни услуги към комплексно реш...beITconference
 
The Web and The Social - Harry Birimirski, Smart IT
The Web and The Social - Harry Birimirski, Smart ITThe Web and The Social - Harry Birimirski, Smart IT
The Web and The Social - Harry Birimirski, Smart ITbeITconference
 

More from beITconference (7)

ADASTRA +1 or How YOU generate additional value in the project lifecycle - Пе...
ADASTRA +1 or How YOU generate additional value in the project lifecycle - Пе...ADASTRA +1 or How YOU generate additional value in the project lifecycle - Пе...
ADASTRA +1 or How YOU generate additional value in the project lifecycle - Пе...
 
NoSQL and Cloud Services - Philip Balinow, Comfo
NoSQL and Cloud Services -  Philip Balinow, ComfoNoSQL and Cloud Services -  Philip Balinow, Comfo
NoSQL and Cloud Services - Philip Balinow, Comfo
 
Mobile First with Angular.JS - Владимир Цветков, Obecto
Mobile First with Angular.JS - Владимир Цветков, ObectoMobile First with Angular.JS - Владимир Цветков, Obecto
Mobile First with Angular.JS - Владимир Цветков, Obecto
 
Уроците от работата ми по WordPress.com - Веселин Николов
Уроците от работата ми по WordPress.com - Веселин НиколовУроците от работата ми по WordPress.com - Веселин Николов
Уроците от работата ми по WordPress.com - Веселин Николов
 
Scrum Crash Course - Anatoli Iliev and Lyubomir Cholakov, Infragistics
Scrum Crash Course - Anatoli Iliev and Lyubomir Cholakov, InfragisticsScrum Crash Course - Anatoli Iliev and Lyubomir Cholakov, Infragistics
Scrum Crash Course - Anatoli Iliev and Lyubomir Cholakov, Infragistics
 
Развитие на финансовите приложения от транзакционни услуги към комплексно реш...
Развитие на финансовите приложения от транзакционни услуги към комплексно реш...Развитие на финансовите приложения от транзакционни услуги към комплексно реш...
Развитие на финансовите приложения от транзакционни услуги към комплексно реш...
 
The Web and The Social - Harry Birimirski, Smart IT
The Web and The Social - Harry Birimirski, Smart ITThe Web and The Social - Harry Birimirski, Smart IT
The Web and The Social - Harry Birimirski, Smart IT
 

Recently uploaded

Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Dutch Power
 
Gregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptxGregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptx
gharris9
 
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdfSupercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Access Innovations, Inc.
 
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptxsomanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
Howard Spence
 
Media as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern EraMedia as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern Era
faizulhassanfaiz1670
 
María Carolina Martínez - eCommerce Day Colombia 2024
María Carolina Martínez - eCommerce Day Colombia 2024María Carolina Martínez - eCommerce Day Colombia 2024
María Carolina Martínez - eCommerce Day Colombia 2024
eCommerce Institute
 
International Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software TestingInternational Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software Testing
Sebastiano Panichella
 
Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...
Sebastiano Panichella
 
Acorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutesAcorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutes
IP ServerOne
 
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
0x01 - Newton's Third Law:  Static vs. Dynamic Abusers0x01 - Newton's Third Law:  Static vs. Dynamic Abusers
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
OWASP Beja
 
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Sebastiano Panichella
 
Tom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issueTom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issue
amekonnen
 
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AwangAniqkmals
 
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
OECD Directorate for Financial and Enterprise Affairs
 
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdfBonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
khadija278284
 
Bitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXOBitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXO
Matjaž Lipuš
 
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Dutch Power
 
Burning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdfBurning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdf
kkirkland2
 
Getting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control TowerGetting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control Tower
Vladimir Samoylov
 
Obesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditionsObesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditions
Faculty of Medicine And Health Sciences
 

Recently uploaded (20)

Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
 
Gregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptxGregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptx
 
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdfSupercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
 
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptxsomanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
 
Media as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern EraMedia as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern Era
 
María Carolina Martínez - eCommerce Day Colombia 2024
María Carolina Martínez - eCommerce Day Colombia 2024María Carolina Martínez - eCommerce Day Colombia 2024
María Carolina Martínez - eCommerce Day Colombia 2024
 
International Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software TestingInternational Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software Testing
 
Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...
 
Acorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutesAcorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutes
 
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
0x01 - Newton's Third Law:  Static vs. Dynamic Abusers0x01 - Newton's Third Law:  Static vs. Dynamic Abusers
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
 
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
 
Tom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issueTom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issue
 
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
AWANG ANIQKMALBIN AWANG TAJUDIN B22080004 ASSIGNMENT 2 MPU3193 PHILOSOPHY AND...
 
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
 
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdfBonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
 
Bitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXOBitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXO
 
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
 
Burning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdfBurning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdf
 
Getting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control TowerGetting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control Tower
 
Obesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditionsObesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditions
 

Unit & Automation Testing in Android - Stanislav Gatsev, Melon

  • 1. Unit and Automation Testing on Android Stanislav Gatsev Android Team Lead, Melon Inc.
  • 2. Who am I? Stanislav Gatsev Android Team Lead +359 89 891 4481 stanislav.gatsev@melontech.com http://www.melon.bg
  • 3. What is Android? o World's most popular mobile platform o Every day another million users power up their Android devices for the first time o Using Android SDK we can develop software for almost everything from smartphones and tablets to glasses, watches, TVs and even cars.
  • 4. Why we automate testing? o We save time !!! o We improve our code quality o We support live documentation o We run our regression tests o We fight fragmentation
  • 6. What Can We Test? o We can test our logic, algorithms, calculations o We can test our UI and navigation logic o We can test IO operations, database and network operations o And more... @SmallTest public void test_addOperatorInJava() throws Exception { Assert.assertEquals(4, 2 + 2); Assert.assertEquals(27, 25 + 2); } @SmallTest public void test_FieldsAreVisible() { ViewAsserts.assertOnScreen(mRootView, mEmailEditText); ViewAsserts.assertOnScreen(mRootView, mPasswordEditText); ViewAsserts.assertOnScreen(mRootView, mLoginActionLayout); }
  • 7. How we automate testing? o We use JUnit o We mock our objects o We are using activity testing framework o We click with Robotium
  • 8. JUnit and Android We can use the JUnit TestCase class to do unit testing on a class that doesn't call Android APIs. TestCase is also the base class for AndroidTestCase, which we can use to test Android-dependent objects. Besides providing the JUnit framework, AndroidTestCase offers Android-specific setup, teardown, and helper methods. public class UtilTests extends AndroidTestCase { @SmallTest public void test_StringToLatLngValidValues() throws Exception { String latLngString = "34.009555,-118.497072"; LatLng latLng = Util.stringToLatLng(latLngString); Assert.assertEquals(Double.compare(latLng.latitude, 34.009555), 0); Assert.assertEquals(Double.compare(latLng.longitude, -118.497072), 0); } @SmallTest public void test_StringToLatLngValidInvalues() throws Exception { String latLngString = "34.009555,-a118.497072"; LatLng latLng = Util.stringToLatLng(latLngString); Assert.assertEquals(Double.compare(latLng.latitude, 0), 0); Assert.assertEquals(Double.compare(latLng.longitude, 0), 0); } }
  • 9. Why we use Mocks? The objective of unit testing is to exercise just one method at a time, but what happens when that method depends on other things—hard-to-control things such as the network, or a database. The solution is the Mock object. It is simply a debug replacement for a real- world object.
  • 10. Mockito Mockito is the way we mock objects in Android. As its developers say: “Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with clean & simple API. Mockito doesn't give you hangover because the tests are very readable and they produce clean verification errors.” • https://code.google.com/p/dexmaker/ • System property hack
  • 11. Mockito and Android //create Mock of server communication final WeatherServerRequest weatherServerRequest = Mockito.mock(WeatherServerRequest.class); //stub the actual network call and return empty object ServerResponse serverResponse = new ServerResponse(); Mockito.when(weatherServerRequest.getResponse()).thenReturn(serverResponse); //create mock of the refresh-able and the observers subject IRefreshable refreshable = Mockito.mock(IRefreshable.class); ISubject subject = Mockito.mock(ISubject.class); runWeatherWorkerThread(refreshable, subject, weatherServerRequest); //verify if refresh-able start callback is called Mockito.verify(refreshable).onStartRefresh(); //verify if the observers callback is called Mockito.verify(subject).updateData(Mockito.anyMapOf(String.class, ServerResponse.class)); //verify if refresh-able end callback is called Mockito.verify(refreshable).onEndRefresh(); =+
  • 12. Activity tests ActivityInstrumentationTestCase2 This is the class which helps us test our Activities. It has direct reference to the tested activity and you have the ability to run whole test or just parts of it in the UI Thread. @SmallTest public void test_viewPagerHasAllLocations() throws Throwable { //Creates the data fetcher mock and injects it to the Activity for every test createDataFetcherMock(); runTestOnUiThread(new Runnable() { @Override public void run() { //reinitialize activity with new data fetcher getActivity().init(); } }); //asserts if the view pager has the same number of items the data fetcher returned Assert.assertEquals(3, mViewPager.getAdapter().getCount()); }
  • 13. Robotium o It is a lot easier to write our automation tests o Helpful API for executing user actions o Flexible results assertion o Hybrid apps are supported o We can use it in our Activity tests
  • 14. Robotium in action @LargeTest public void test_addNewLocation() throws Throwable { getInstrumentation().waitForIdleSync(); moveMapToPosition(); getInstrumentation().waitForIdleSync(); //gets the root view of the Activity View rootView = getActivity().findViewById(android.R.id.content); //long click in the center of the screen mSolo.clickLongOnScreen(rootView.getWidth()/2, rootView.getHeight()/2); getInstrumentation().waitForIdleSync(); //get first weather location WeatherDataFetcher dataFetcher = getActivity().getDataFetcher(); List<WeatherLocation> weatherLocations = dataFetcher.getLocationsList(); Assert.assertEquals(1, weatherLocations.size()); //check if the location is the right one WeatherLocation weatherLocation = weatherLocations.get(0); Assert.assertEquals("Santa Monica, CA", weatherLocation.getLocationName()); //clears DB dataFetcher.removeLocation(0); }
  • 16. References o https://developer.android.com o https://code.google.com/p/robotium/ o https://github.com/mockito/mockito o http://blog.gfader.com/2010/10/why-are-automated-tests-so-important.html o http://media.pragprog.com/titles/utj/mockobjects.pdf o http://www.embedded.com/design/prototyping-and- development/4398723/The-mock-object-approach-to-test-driven- development