SlideShare uma empresa Scribd logo
1 de 18
#android #unit-testing
Robolectric Adventure v.2
Agenda
1. Different types of testing
2. How does Robolectric
work?
3. How to write Robolectric
tests?
4. Q&A
How to check the bicycle?
How to check the chain transmission?
How to check the ball bearing?
android.jar
V
V
V
X
X
Shadowing
AOSP
In addition
• Resource loading
• Controlling main and background loopers
• Access for private state for Android classes
• Test application
Run first test
• Modify build.gradle
• Add test runner with configuration
• Write test code
• Run grade
build.grade
buildscript {
depednencies {
classpath 'org.robolectric:robolectric-gradle-
plugin:1.0.1'
}
}
apply plugin: 'org.robolectric'
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
}
Test
@RunWith( RobolectricTestRunner.class )
@Config( emulateSdk = 19 )
public class AboutActivityTest
{
private AboutActivity activity;
@Before
public void setUp()
{
activity = Robolectric.buildActivity( AboutActivity.class ).create().get();
}
@Test
public void copyrightYearIsCorrect()
throws Exception
{
int year = Calendar.getInstance().get( Calendar.YEAR );
String text = String.format(
activity.getString( R.string.Copyright_Minddistrict ),
year
);
assertThat( activity.copyrightText ).hasTextString( text );
}
}
AssertJ/FestAssert
assertThat(view).isGone();
vs
assertEquals(View.GONE, view.getVisibility());
Expected visibility <gone> but was <invisible>.
vs
expected: <8> but was: <4>
Behaviour test
public class LoginActivity
extends ActionBarActivity
{
User user;
@InjectView( R.id.email_address )
EditText emailField;
@InjectView( R.id.password )
EditText passwordField;
@OnClick( R.id.button_login )
public void login()
{
if ( validateForm() )
{
user.startDiscovery( emailField.getText().toString(),
passwordField.getText().toString() );
}
}
}
Mockito
public class LoginActivityTest
{
private static final String EMAIL = "test@gas.com";
private static final String PASSWORD = "pass";
LoginActivity activity;
@Test
public void whenLoginStartedThenUserCalled()
throws Exception
{
activity.emailField.setText( EMAIL );
activity.passwordField.setText( PASSWORD );
User user = mock( User.class );
activity.user = user;
activity.login();
verify( user ).startDiscovery( EMAIL, PASSWORD );
}
}
Test application
<application android:name=".FooApplication">
public class FooApplication
extends Application {
@Override
public void onCreate() {
super.onCreate();
// Dependency injection init
// Analytics setup
// ORM configuration
}
}
public class TestFooApplication
extends FooApplication {
@Override
public void onCreate() {
super.onCreate();
// Surpress ORM
// Configure test injections
// Disabling analytics
}
}
Future
• Android gradle plugin 1.1
• Easy tests in Android Studio
• Robolectric 3.0
References
• Answers about different types of testing (http://www.quora.com/What-is-
the-difference-between-unit-testing-functional-testing-and-integration-
testing)
• Talk about how Robolectric works (http://pivotallabs.com/mike-grafton-
robolectric/ )
• Sample project how setup Robolectric, Android Studio and android
grade plugin (https://github.com/nenick/AndroidStudioAndRobolectric)
• Robolectric github page (https://github.com/robolectric)
• Robolectric official web page (http://robolectric.org/)
• My twitter for any question or help (@jack_martynov)
Thank you!
Any question?

Mais conteúdo relacionado

Mais procurados

Testing Salt States (part 1)
Testing Salt States (part 1)Testing Salt States (part 1)
Testing Salt States (part 1)
jasondenning
 

Mais procurados (20)

Angular 2... so can I use it now??
Angular 2... so can I use it now??Angular 2... so can I use it now??
Angular 2... so can I use it now??
 
Testing Salt States (part 1)
Testing Salt States (part 1)Testing Salt States (part 1)
Testing Salt States (part 1)
 
Adventures with Angular 2
Adventures with Angular 2Adventures with Angular 2
Adventures with Angular 2
 
JavaFX8 TestFX - CDI
JavaFX8   TestFX - CDIJavaFX8   TestFX - CDI
JavaFX8 TestFX - CDI
 
How to create your first project in spring using eclipse
How to create your first project in spring using eclipseHow to create your first project in spring using eclipse
How to create your first project in spring using eclipse
 
Maintaining Your Tests At Scale
Maintaining Your Tests At ScaleMaintaining Your Tests At Scale
Maintaining Your Tests At Scale
 
Branching Strategies For Git and Subversion
Branching Strategies For Git and SubversionBranching Strategies For Git and Subversion
Branching Strategies For Git and Subversion
 
Retrolambda+bolts
Retrolambda+boltsRetrolambda+bolts
Retrolambda+bolts
 
Unit Testing your React / Redux app (@BucharestJS)
Unit Testing your React / Redux app (@BucharestJS)Unit Testing your React / Redux app (@BucharestJS)
Unit Testing your React / Redux app (@BucharestJS)
 
Test driving QML
Test driving QMLTest driving QML
Test driving QML
 
Gatling
Gatling Gatling
Gatling
 
Test driving-qml
Test driving-qmlTest driving-qml
Test driving-qml
 
Reactive Streams and RxJava2
Reactive Streams and RxJava2Reactive Streams and RxJava2
Reactive Streams and RxJava2
 
Build Automation in Android
Build Automation in AndroidBuild Automation in Android
Build Automation in Android
 
OpenDaylight Developers Experience 1.5: Eclipse Setup, HOT reload, future plans
OpenDaylight Developers Experience 1.5: Eclipse Setup, HOT reload, future plansOpenDaylight Developers Experience 1.5: Eclipse Setup, HOT reload, future plans
OpenDaylight Developers Experience 1.5: Eclipse Setup, HOT reload, future plans
 
Angular 2 + TypeScript = true. Let's Play!
Angular 2 + TypeScript = true. Let's Play!Angular 2 + TypeScript = true. Let's Play!
Angular 2 + TypeScript = true. Let's Play!
 
Test pyramid in an Android Agile Project - TDC2013
Test pyramid in an Android Agile Project - TDC2013Test pyramid in an Android Agile Project - TDC2013
Test pyramid in an Android Agile Project - TDC2013
 
Никита Галкин "Testing in Frontend World"
Никита Галкин "Testing in Frontend World"Никита Галкин "Testing in Frontend World"
Никита Галкин "Testing in Frontend World"
 
Gatling - Stress test tool
Gatling - Stress test toolGatling - Stress test tool
Gatling - Stress test tool
 
Tugbot - Testing Framework for Docker Containers
Tugbot - Testing Framework for Docker ContainersTugbot - Testing Framework for Docker Containers
Tugbot - Testing Framework for Docker Containers
 

Semelhante a Robolectric v2

Semelhante a Robolectric v2 (20)

Android Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan KustAndroid Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
 
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan KustInfinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
 
Javascript tdd byandreapaciolla
Javascript tdd byandreapaciollaJavascript tdd byandreapaciolla
Javascript tdd byandreapaciolla
 
Testcontainers - Geekout EE 2017 presentation
Testcontainers - Geekout EE 2017 presentationTestcontainers - Geekout EE 2017 presentation
Testcontainers - Geekout EE 2017 presentation
 
Unit testing and Android
Unit testing and AndroidUnit testing and Android
Unit testing and Android
 
Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!
 
Java 9 preview
Java 9 previewJava 9 preview
Java 9 preview
 
Reproducible component tests using docker
Reproducible component tests using dockerReproducible component tests using docker
Reproducible component tests using docker
 
Gradle
GradleGradle
Gradle
 
Android gradle-build-system-overview
Android gradle-build-system-overviewAndroid gradle-build-system-overview
Android gradle-build-system-overview
 
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
 
Java9 and the impact on Maven Projects (JFall 2016)
Java9 and the impact on Maven Projects (JFall 2016)Java9 and the impact on Maven Projects (JFall 2016)
Java9 and the impact on Maven Projects (JFall 2016)
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
What we can expect from Java 9 by Ivan Krylov
What we can expect from Java 9 by Ivan KrylovWhat we can expect from Java 9 by Ivan Krylov
What we can expect from Java 9 by Ivan Krylov
 
Robolectric Adventure
Robolectric AdventureRobolectric Adventure
Robolectric Adventure
 
Test Automation using Ruby
Test Automation using Ruby Test Automation using Ruby
Test Automation using Ruby
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginMastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
 
Robolectric android taipei
Robolectric   android taipeiRobolectric   android taipei
Robolectric android taipei
 
Building XWiki
Building XWikiBuilding XWiki
Building XWiki
 
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-JupiterToolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
 

Mais de Eugen Martynov

Mais de Eugen Martynov (12)

Kotlin Script
Kotlin ScriptKotlin Script
Kotlin Script
 
My path to freelance
My path to freelanceMy path to freelance
My path to freelance
 
Gradle Again
Gradle AgainGradle Again
Gradle Again
 
Android CD
Android CDAndroid CD
Android CD
 
Lokalise
LokaliseLokalise
Lokalise
 
DI with Dagger2
DI with Dagger2DI with Dagger2
DI with Dagger2
 
Facebook Stetho
Facebook StethoFacebook Stetho
Facebook Stetho
 
Template project
Template projectTemplate project
Template project
 
Android Brown Bag Lunch - DroidconNL overview
Android Brown Bag Lunch - DroidconNL overviewAndroid Brown Bag Lunch - DroidconNL overview
Android Brown Bag Lunch - DroidconNL overview
 
Flow
FlowFlow
Flow
 
Mobile developer is Software developer
Mobile developer is Software developerMobile developer is Software developer
Mobile developer is Software developer
 
XP Days UA Pecha kucha
XP Days UA Pecha kuchaXP Days UA Pecha kucha
XP Days UA Pecha kucha
 

Último

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Último (20)

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Robolectric v2

  • 2. Agenda 1. Different types of testing 2. How does Robolectric work? 3. How to write Robolectric tests? 4. Q&A
  • 3. How to check the bicycle?
  • 4. How to check the chain transmission?
  • 5. How to check the ball bearing?
  • 8. In addition • Resource loading • Controlling main and background loopers • Access for private state for Android classes • Test application
  • 9. Run first test • Modify build.gradle • Add test runner with configuration • Write test code • Run grade
  • 10. build.grade buildscript { depednencies { classpath 'org.robolectric:robolectric-gradle- plugin:1.0.1' } } apply plugin: 'org.robolectric' dependencies { testCompile 'junit:junit:4.12' testCompile 'org.robolectric:robolectric:2.4' }
  • 11. Test @RunWith( RobolectricTestRunner.class ) @Config( emulateSdk = 19 ) public class AboutActivityTest { private AboutActivity activity; @Before public void setUp() { activity = Robolectric.buildActivity( AboutActivity.class ).create().get(); } @Test public void copyrightYearIsCorrect() throws Exception { int year = Calendar.getInstance().get( Calendar.YEAR ); String text = String.format( activity.getString( R.string.Copyright_Minddistrict ), year ); assertThat( activity.copyrightText ).hasTextString( text ); } }
  • 13. Behaviour test public class LoginActivity extends ActionBarActivity { User user; @InjectView( R.id.email_address ) EditText emailField; @InjectView( R.id.password ) EditText passwordField; @OnClick( R.id.button_login ) public void login() { if ( validateForm() ) { user.startDiscovery( emailField.getText().toString(), passwordField.getText().toString() ); } } }
  • 14. Mockito public class LoginActivityTest { private static final String EMAIL = "test@gas.com"; private static final String PASSWORD = "pass"; LoginActivity activity; @Test public void whenLoginStartedThenUserCalled() throws Exception { activity.emailField.setText( EMAIL ); activity.passwordField.setText( PASSWORD ); User user = mock( User.class ); activity.user = user; activity.login(); verify( user ).startDiscovery( EMAIL, PASSWORD ); } }
  • 15. Test application <application android:name=".FooApplication"> public class FooApplication extends Application { @Override public void onCreate() { super.onCreate(); // Dependency injection init // Analytics setup // ORM configuration } } public class TestFooApplication extends FooApplication { @Override public void onCreate() { super.onCreate(); // Surpress ORM // Configure test injections // Disabling analytics } }
  • 16. Future • Android gradle plugin 1.1 • Easy tests in Android Studio • Robolectric 3.0
  • 17. References • Answers about different types of testing (http://www.quora.com/What-is- the-difference-between-unit-testing-functional-testing-and-integration- testing) • Talk about how Robolectric works (http://pivotallabs.com/mike-grafton- robolectric/ ) • Sample project how setup Robolectric, Android Studio and android grade plugin (https://github.com/nenick/AndroidStudioAndRobolectric) • Robolectric github page (https://github.com/robolectric) • Robolectric official web page (http://robolectric.org/) • My twitter for any question or help (@jack_martynov)