Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Testing Android Apps

felipecsl
August 23, 2014

Unit Testing Android Apps

felipecsl

August 23, 2014
Tweet

More Decks by felipecsl

Other Decks in Programming

Transcript

  1. O que causa Débito Técnico? The three sins of software

    development: http://blog.ionelmc.ro/2014/08/14/the-three-sins-of-software-development/
  2. build.gradle buildscript { repositories { mavenCentral() } dependencies { classpath

    'org.robolectric:robolectric-gradle-plugin:0.12.0' } } apply plugin: 'robolectric'
  3. build.gradle android { defaultConfig { // optional testInstrumentationRunner "com.example.MyAppTestRunner" }

    robolectric { include '**/*Test.class' } // optional sourceSets { androidTest { setRoot('src/test') } } }
  4. build.gradle dependencies { compile 'com.android.support:appcompat-v7:20.0.0' // other dev dependencies... androidTestCompile

    'junit:junit:4.10' androidTestCompile 'org.robolectric:robolectric:2.4-SNAPSHOT' androidTestCompile 'org.mockito:mockito-core:1.9.5' androidTestCompile 'org.hamcrest:hamcrest-library:1.3' }
  5. JUnit + Hamcrest @Config(emulateSdk = 18) @RunWith(MyTestRunner.class) public class UserTest

    { private User user; @Before public void setUp() { user = new User(); user.setId(3); } @Test public void userShouldPersistFields() { assertFalse(user.isPublicAccount()); assertThat(user.getId(), equalTo(3)); } }
  6. Robolectric @RunWith(RobolectricTestRunner.class) @Test public void shouldHaveALogo() throws Exception { ImageView

    pivotalLogo = (ImageView) activity.findViewById(R.id.pivotal_logo); ShadowImageView shadowPivotalLogo = Robolectric.shadowOf(pivotalLogo); assertThat(shadowPivotalLogo.resourceId, equalTo(R.drawable.pivotallabs_logo)); }
  7. Mockito @Before public void setUp() { HttpClient httpClient = mock(httpClient.class);

    ApiEndpoint endpoint = new ApiEndpoint(httpClient); } @Test public void userShouldPersistFields() { when(httpClient.get("/users/2")).thenReturn(new User()); endpoint.getUser(2); verify(httpClient).get("/users/2"); }
  8. Feature: Rating a stand Scenario: Find and rate a stand

    from the list Given I am on the foodstand list Then I should see a "rating" button Then I should not see "Dixie Burger & Gumbo Soup" When I touch the "rating" button Then I should see "Dixie Burger & Gumbo Soup" When I touch "Dixie Burger & Gumbo Soup" Then I should see details for "Dixie Burger & Gumbo Soup" When I touch the "rate_it" button Then I should see the rating panel When I touch "star5" And I touch "rate" Then "Dixie Burger & Gumbo Soup" should be rated 5 stars