Robolectric v2
-
Upload
eugen-martynov -
Category
Software
-
view
83 -
download
3
Transcript of Robolectric v2
![Page 1: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/1.jpg)
#android #unit-testing Robolectric Adventure v.2
![Page 2: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/2.jpg)
Agenda1. Different types of testing
2. How does Robolectric
work?
3. How to write Robolectric
tests?
4. Q&A
![Page 3: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/3.jpg)
How to check the bicycle?
![Page 4: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/4.jpg)
How to check the chain transmission?
![Page 5: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/5.jpg)
How to check the ball bearing?
![Page 6: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/6.jpg)
android.jar
V
V
V
X
X
![Page 7: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/7.jpg)
Shadowing
AOSP
![Page 8: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/8.jpg)
In addition
• Resource loading
• Controlling main and background loopers
• Access for private state for Android classes
• Test application
![Page 9: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/9.jpg)
Run first test
• Modify build.gradle
• Add test runner with configuration
• Write test code
• Run grade
![Page 10: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/10.jpg)
build.gradebuildscript {
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'
}
![Page 11: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/11.jpg)
Test@RunWith( RobolectricTestRunner.class )@Config( emulateSdk = 19 )public class AboutActivityTest{
private AboutActivity activity;
@Beforepublic void setUp() {
activity = Robolectric.buildActivity( AboutActivity.class ).create().get();}
@Testpublic 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 );}
}
![Page 12: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/12.jpg)
AssertJ/FestAssert
assertThat(view).isGone();
vs
assertEquals(View.GONE, view.getVisibility());
Expected visibility <gone> but was <invisible>.
vs
expected: <8> but was: <4>
![Page 13: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/13.jpg)
Behaviour testpublic 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() );
}}
}
![Page 14: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/14.jpg)
Mockitopublic class LoginActivityTest{
private static final String EMAIL = "[email protected]";private static final String PASSWORD = "pass";
LoginActivity activity;
@Testpublic 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 );}
}
![Page 15: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/15.jpg)
Test application
<application android:name=".FooApplication">
public class FooApplication extends Application {
@Overridepublic void onCreate() {
super.onCreate();
// Dependency injection init// Analytics setup// ORM configuration
}}
public class TestFooApplication extends FooApplication {
@Overridepublic void onCreate() {
super.onCreate();
// Surpress ORM// Configure test injections// Disabling analytics
}}
![Page 16: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/16.jpg)
Future
• Android gradle plugin 1.1
• Easy tests in Android Studio
• Robolectric 3.0
![Page 17: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/17.jpg)
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)
![Page 18: Robolectric v2](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a87acd1a28abd12b8b4717/html5/thumbnails/18.jpg)
Thank you!
Any question?