SlideShare a Scribd company logo
Robolectric Survival Guide
eBuddy
• 2009 Chat (IM)
• 2011 XMS
• 2012 We started with Robolectric




ANDROID PRODUCTS HISTORY
Android!
Relationships
•   Java
•   Maven
•   Android
•   Unit tests
First Android JUnit Test
import org.junit.Before;
import org.junit.Test;
import static org.fest.assertions.api.Assertions.assertThat;

public class MainActivityTest {
   private MainActivity activity;

    @Before public void setUp () throws Exception
    {
        activity = new MainActivity();
        activity.onCreate( null );
    }

    @Test public void checkInitialTexts () throws Exception
    {
        assertThat( activity.downloadButton.getText() ).isEqualTo( "Download" );
    }
}
First Android JUnit Test
Android Testing Options
•   No tests
•   POJO
•   Instrumental tests
•   Android sources
•   Mock Android
•   Robolectric!
What is inside
• Parses resources
• Intercepts loading Android
  classes
• Rewrites method bodies
  with Javassist
• Binds shadow objects to
  new Android objects
• Proxies modified objects
First Robolectric Test
Key Points from Demo #1
• Correct path to SDK in the
  local.properties
• JUnit dependency before
  Android (IntelliJ IDEA)
• @RunWith(
  RobolectricTestRunner.class)
Biggest Issue of Robolectric

 •   Lack of documentation
 •   Not many examples
 •   Unscheduled releases
 •   http://robolectric.blogspot.nl/
     2011/02/how-to-work-on-
     robolectric-in-parallel.html
Robolectric Drawbacks
• Sometimes magic
• PowerMock integration
• Time of run for the first test
Shadowing
• http://robolectric.blogspot.nl/2
  011/01/how-to-create-your-
  own-shadow-classes.html
• Bind shadow class
• Use own Runner instead of
  Robolectric
• (Submit pull request)
Dependency Injection
RoboGuice 2.0

public class MainActivity extends RoboActivity implements View.OnClickListener
{
    @Inject Bus bus;

    @Override public void onCreate( Bundle savedInstanceState )
    {
        bus.register( this );
    }

    @Override public void onClick( View view )
    {
        bus.post( new DownloadDataEvent( urlInput.getText().toString() ) );
    }
}
RoboGuice 2.0
Key Points from Demo #2
• InjectingTestRunner
• Own AbstractModule
  implementation
• Run injections before
  every test method
Android Real Jar (Robolectric 2.0)
• http://robolectric.blogspot.nl
  /2011/08/using-robolectric-
  with-real-android.html
• Robolectric 2.0 alpha
Robolectric or not Robolectric
• Not a question for me
• Follow industry
Thank you!

More Related Content

PDF
Learn How to Unit Test Your Android Application (with Robolectric)
PDF
Unit testing and Android
PDF
Automation Abstraction Layers: Page Objects and Beyond
PDF
iOS UI Testing in Xcode
PDF
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
PPTX
iOS Automation: XCUITest + Gherkin
Learn How to Unit Test Your Android Application (with Robolectric)
Unit testing and Android
Automation Abstraction Layers: Page Objects and Beyond
iOS UI Testing in Xcode
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
iOS Automation: XCUITest + Gherkin

What's hot (17)

PDF
Page Objects Done Right - selenium conference 2014
PPTX
XCUITest for iOS App Testing and how to test with Xcode
PPTX
Secret unit testing tools
PDF
Android Building, Testing and reversing
PDF
Ten Minutes To Tellurium
PPT
Singleton Object Management
PDF
基於 Flow & Path 的 MVP 架構
PDF
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
PDF
Automated Xcode 7 UI Testing
PDF
crashing in style
PPT
A journey beyond the page object pattern
PDF
Builda responsivetypescriptwebdriverio framework
PDF
Selenium - The page object pattern
PDF
React native: building native iOS apps with javascript
PDF
Writing Android Libraries
PDF
Automation Abstractions: Page Objects and Beyond
PDF
The Many Ways to Test Your React App
Page Objects Done Right - selenium conference 2014
XCUITest for iOS App Testing and how to test with Xcode
Secret unit testing tools
Android Building, Testing and reversing
Ten Minutes To Tellurium
Singleton Object Management
基於 Flow & Path 的 MVP 架構
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
Automated Xcode 7 UI Testing
crashing in style
A journey beyond the page object pattern
Builda responsivetypescriptwebdriverio framework
Selenium - The page object pattern
React native: building native iOS apps with javascript
Writing Android Libraries
Automation Abstractions: Page Objects and Beyond
The Many Ways to Test Your React App
Ad

Viewers also liked (6)

PPTX
Animals test
PPTX
My summer experience with the future
PPT
Android Brown Bag Lunch - DroidconNL overview
PPTX
Organisasi belajar mendunia
PPTX
Facebook Stetho
PPTX
Template project
Animals test
My summer experience with the future
Android Brown Bag Lunch - DroidconNL overview
Organisasi belajar mendunia
Facebook Stetho
Template project
Ad

Similar to Robolectric Adventure (20)

PDF
Robolectric android taipei
PDF
Droid con 2013 workshop unit testing in android [robolectirc]
PPTX
Robolectric v2
PDF
Testing on Android
PDF
[Ultracode Munich #4] Short introduction to the new Android build system incl...
PDF
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
PDF
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
PDF
Robotium - sampath
ODP
Unit Test Android Without Going Bald
PPTX
Unit Testing Android Applications
ODP
Testing in Android: automatici, di integrazione, TDD e scenari avanzati
ODP
Android Test Driven Development
PPTX
Android Testing
PPTX
Unit testing without Robolectric, Droidcon Berlin 2016
PDF
How ANDROID TESTING changed how we think about Death - Second Edition
PDF
How ANDROID TESTING changed how we think about Death - Second Edition
PDF
Amplify - TDD on Android with Robolectric
PDF
Introduction to Robotium
PDF
Android TDD
PPTX
Android developer's toolbox
Robolectric android taipei
Droid con 2013 workshop unit testing in android [robolectirc]
Robolectric v2
Testing on Android
[Ultracode Munich #4] Short introduction to the new Android build system incl...
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
Robotium - sampath
Unit Test Android Without Going Bald
Unit Testing Android Applications
Testing in Android: automatici, di integrazione, TDD e scenari avanzati
Android Test Driven Development
Android Testing
Unit testing without Robolectric, Droidcon Berlin 2016
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
Amplify - TDD on Android with Robolectric
Introduction to Robotium
Android TDD
Android developer's toolbox

More from Eugen Martynov (9)

PDF
Kotlin Script
PPTX
My path to freelance
PPTX
Gradle Again
PPTX
Android CD
PPTX
Lokalise
PDF
DI with Dagger2
PPTX
Mobile developer is Software developer
PPT
XP Days UA Pecha kucha
Kotlin Script
My path to freelance
Gradle Again
Android CD
Lokalise
DI with Dagger2
Mobile developer is Software developer
XP Days UA Pecha kucha

Robolectric Adventure

  • 2. eBuddy • 2009 Chat (IM) • 2011 XMS • 2012 We started with Robolectric ANDROID PRODUCTS HISTORY
  • 4. Relationships • Java • Maven • Android • Unit tests
  • 5. First Android JUnit Test import org.junit.Before; import org.junit.Test; import static org.fest.assertions.api.Assertions.assertThat; public class MainActivityTest { private MainActivity activity; @Before public void setUp () throws Exception { activity = new MainActivity(); activity.onCreate( null ); } @Test public void checkInitialTexts () throws Exception { assertThat( activity.downloadButton.getText() ).isEqualTo( "Download" ); } }
  • 7. Android Testing Options • No tests • POJO • Instrumental tests • Android sources • Mock Android • Robolectric!
  • 8. What is inside • Parses resources • Intercepts loading Android classes • Rewrites method bodies with Javassist • Binds shadow objects to new Android objects • Proxies modified objects
  • 10. Key Points from Demo #1 • Correct path to SDK in the local.properties • JUnit dependency before Android (IntelliJ IDEA) • @RunWith( RobolectricTestRunner.class)
  • 11. Biggest Issue of Robolectric • Lack of documentation • Not many examples • Unscheduled releases • http://robolectric.blogspot.nl/ 2011/02/how-to-work-on- robolectric-in-parallel.html
  • 12. Robolectric Drawbacks • Sometimes magic • PowerMock integration • Time of run for the first test
  • 13. Shadowing • http://robolectric.blogspot.nl/2 011/01/how-to-create-your- own-shadow-classes.html • Bind shadow class • Use own Runner instead of Robolectric • (Submit pull request)
  • 15. RoboGuice 2.0 public class MainActivity extends RoboActivity implements View.OnClickListener { @Inject Bus bus; @Override public void onCreate( Bundle savedInstanceState ) { bus.register( this ); } @Override public void onClick( View view ) { bus.post( new DownloadDataEvent( urlInput.getText().toString() ) ); } }
  • 17. Key Points from Demo #2 • InjectingTestRunner • Own AbstractModule implementation • Run injections before every test method
  • 18. Android Real Jar (Robolectric 2.0) • http://robolectric.blogspot.nl /2011/08/using-robolectric- with-real-android.html • Robolectric 2.0 alpha
  • 19. Robolectric or not Robolectric • Not a question for me • Follow industry