Robospock droidcon '14

of 28/28
Applause www.applause.com Crowd Testing Beta Distributions Crash Reports Application Analytics We’re hiring!
  • date post

    15-Aug-2015
  • Category

    Software

  • view

    17
  • download

    1

Embed Size (px)

Transcript of Robospock droidcon '14

  1. 1. Applause www.applause.com Crowd Testing Beta Distributions Crash Reports Application Analytics Were hiring!
  2. 2. Przemek Jakubczyk Senior Android Developer Applause Inc [email protected] GH: pjakubczyk
  3. 3. Robospock
  4. 4. def "should change label on button click"(){ given: def button = new Button() when: button.click() then: button.getLabel() == "The new label" }
  5. 5. def "should change label on button click"(){ given: def button = new Button() when: button.click() then: button.getLabel() == "The new label" } @Test public void shouldChangeLabelOnButtonClick(){ //given Button button = new Button(); // when button.click(); // then Assert.assertEquals("The new label", button.getLabel()); }
  6. 6. def "should throw RuntimeException on button click"(){ given: def button = new Button() when: button.click() then: thrown(RuntimeException) }
  7. 7. def "should throw RuntimeException on button click"(){ given: def button = new Button() when: button.click() then: thrown(RuntimeException) } @Test(expected = RuntimeException.class) public void shouldThrowRuntimeExceptionOnButtonClick(){ //given Button button = new Button(); // when button.click(); // then // look at the @Test annotation
  8. 8. def "should rename the file on button click"(){ given: def button = new Button() and: def file = Mock(File) button.setFile(file) when: button.click() then: 1 * file.renameTo(_) }
  9. 9. @Test public void shouldRenameTheFileOnButtonClick(){ // given Button button = new Button(); // and File file = Mockito.mock(File.class); button.setFile(file); // when button.click(); // then Mockito.verify(file).renameTo(Matchers.anyString()); }
  10. 10. def "should return first element from list"(){ given: def list = Mock(List) { get(0) >> "First" } expect: list.get(0) == "First" }
  11. 11. @Test public void shouldReturnFirstElementFromList() { // given List list = Mockito.mock(List.class); Mockito.when(list.get(0)).thenReturn("First"); // expect Assert.assertEquals("First", list.get(0)); } def "should return first element from list"(){ given: def list = Mock(List) { get(0) >> "First" } expect: list.get(0) == "First" }
  12. 12. def "maximum of two numbers"() { expect: Math.max(first, second) == result where: first | second | result 3 | 5 | 5 7 | 0 | 7 0 | 0 | 0 }
  13. 13. @Test(dataProvider = "provideNumbers") public void maximumOfTwoNumber(int first, int second, int result) { Assert.assertEquals(Math.max(first, second), result); } @DataProvider(name = "provideNumbers") public Object[][] provideData() { return new Object[][] { { 3, 5, 5 }, { 7, 0, 7 }, { 0, 0, 0 } }; }
  14. 14. def "should change label on button click"(){ given: "create new button" when: "perform the click action" then: "the label has changed" }
  15. 15. def "should change label on button click"(){ given: "create new button" def button = new Button() when: "perform the click action" button.click() then: "the label has changed" button.getLabel() == "The new label" }
  16. 16. Because we have Groovy (1..10).collect{new User()} def "should return first element from list"(){} new File("local.properties").text ["first", "second", "third"].each { println it } users.find { it.name == "Adam" }
  17. 17. It was Spock and Groovy
  18. 18. Where is Android?
  19. 19. Robolectric + Spock = RoboSpock
  20. 20. def "should show a toast on button click"(){ given: def button = new MyButton(Robolectric.application) when: button.performClick() then: ShadowToast.textOfLatestToast =~ "Part of the toast" }
  21. 21. def "should do the login action"(){ given: "create new login screen" def loginCompoundView = new LoginCompoundView(Robolectric.application) and: "set anonymous user" loginCompoundView.setEmail("[email protected]") and: "mock internet connector" loginCompoundView.apiConnector = Mock(ApiConnector) when: "perform the login action" loginCompoundView.performLogn() then: "check if the title has changed" loginCompoundView.title == "Authorisation in progress ..." and: "the api connector has been called" 1 * loginCompoundView.apiConnector.doLogin("[email protected]", _) }
  22. 22. Why it is worth to try Groovy is colorful and funny Strict test format given/when/then Human readable as hell BDD
  23. 23. You are not losing anything Robolectric Based on jUnit Full support with Java tools like jacoco XML/HTML reports for CI
  24. 24. How to start? Make it run with gradle What is my first test What should I test
  25. 25. Huston, we have a problem Robolectric configuration is not easy RoboSpock as well https://github.com/pelotasplus/RoboSpock- tutorial
  26. 26. Whats next Site to update: www.robospock.org Report issue: https://github.com/robospock
  27. 27. QA Przemek Jakubczyk Senior Android Developer Applause Inc [email protected] GH: pjakubczyk
  28. 28. Applause We are hiring! Please send your referrals to: [email protected]