robolectric adventure
DESCRIPTION
My Robolectric overview done on Android meet-up hosted by Xebia The final code from the presentation with additional examples will be here: https://github.com/emartynov/robolectric-presentationTRANSCRIPT
![Page 1: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/1.jpg)
Robolectric Survival Guide
![Page 2: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/2.jpg)
ANDROID PRODUCTS HISTORY
• 2009 Chat (IM)• 2011 XMS• 2012 We started with Robolectric
eBuddy
![Page 3: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/3.jpg)
Android!
![Page 4: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/4.jpg)
Relationships
• Java• Maven• Android• Unit tests
![Page 5: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/5.jpg)
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" ); } }
![Page 6: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/6.jpg)
First Android JUnit Test
![Page 7: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/7.jpg)
Android Testing Options
• No tests• POJO• Instrumental tests• Android sources• Mock Android• Robolectric!
![Page 8: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/8.jpg)
What is inside
• Parses resources• Intercepts loading Android
classes • Rewrites method bodies
with Javassist• Binds shadow objects to
new Android objects• Proxies modified objects
![Page 9: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/9.jpg)
First Robolectric Test
![Page 10: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/10.jpg)
Key Points from Demo #1
• Correct path to SDK in the local.properties
• JUnit dependency before Android (IntelliJ IDEA)
• @RunWith(
RobolectricTestRunner.class)
![Page 11: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/11.jpg)
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
![Page 12: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/12.jpg)
Robolectric Drawbacks
• Sometimes magic • PowerMock integration• Time of run for the first test
![Page 13: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/13.jpg)
Shadowing
• http://robolectric.blogspot.nl/2011/01/how-to-create-your-own-shadow-classes.html
• Bind shadow class• Use own Runner instead of
Robolectric• (Submit pull request)
![Page 14: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/14.jpg)
Dependency Injection
![Page 15: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/15.jpg)
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() ) ); } }
![Page 16: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/16.jpg)
RoboGuice 2.0
![Page 17: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/17.jpg)
Key Points from Demo #2
• InjectingTestRunner• Own AbstractModule
implementation• Run injections before
every test method
![Page 18: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/18.jpg)
Android Real Jar (Robolectric 2.0)
• http://robolectric.blogspot.nl/2011/08/using-robolectric-with-real-android.html
• Robolectric 2.0 alpha
![Page 19: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/19.jpg)
Robolectric or not Robolectric
• Not a question for me• Follow industry
![Page 20: Robolectric Adventure](https://reader036.vdocuments.us/reader036/viewer/2022062513/554f6018b4c905bb178b4676/html5/thumbnails/20.jpg)
Thank you!