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

Groovy/Spock for Android app testing

Groovy/Spock for Android app testing

Slide for potatotips #22 http://connpass.com/event/20240/

---

Use Spock testing framework for unitt test on Android.

Masayuki Izumi

October 13, 2015
Tweet

More Decks by Masayuki Izumi

Other Decks in Programming

Transcript

  1.  > Masayuki IZUMI a.k.a. @izumin5210 > Rekimoto Lab. at

    the University of Tokyo > Wantedly, Inc. > Rubyist / Androider / {Java,Type}Scripter
  2. public class GreeterTest { @Test public void sayHello() throws Exception

    { Greeter greeter = new Greeter(); assertEquals("Hello", greeter.say()); } }
  3. public class GreeterTest { private Greeter greeter; @Before public void

    setUp() throws Exception { greeter = new Greeter(); } @Test public void sayMorning() throws Exception { assertEquals("Good morning", greeter.say("morning")); } @Test public void sayHello() throws Exception { assertEquals("Hello", greeter.say("afternoon")); } }
  4. public class GreeterTest { private Greeter greeter; @Before public void

    setUp() throws Exception { greeter = new Greeter(); } @Test public void sayMorning() throws Exception { assertEquals("Good morning", greeter.say("morning")); } @Test public void sayHello() throws Exception { assertEquals("Hello", greeter.say("afternoon")); } @Test public void sayNight() throws Exception { assertEquals("Good night", greeter.say("night")); } }
  5. public class GreeterTest { @Test public void say() throws Exception

    { Greeter greeter = new Greeter(); String[][] pairs = { {"morning", "Good morning"}, {"afternoon", "Hello"}, {"night", "Good night"} }; for (String[] pair : pairs) { assertEquals(pair[1], greeter.say(pair[0])); } } }
  6. @RunWith(Theories.class) public class GreeterTest { public static class Fixture {

    /* ... */ } @DataPoints public static Fixture[] getParameters() { return new Fixture[] { new Fixture("morning", "Good morning"), /* ... */ }; } @Theory public void say(Fixture fixture) throws Exception { Greeter greeter = new Greeter(); assertEquals(fixture.getExpected(), greeter.say(fixture.getArg())); } }
  7. Parameterized test with JUnit  Too complecated and verbose 

    Stop execution when assertion failed - run -> fail -> fix -> run -> fail -> fix -> run -> fail ...
  8. Spock  Testing framework written in Groovy  Inspired from

    JUnit, RSpec, Mockito, ...  Very fluent specification DSL (like RSpec?)  Descriptive assertion message (Power Assert!!!)
  9. Installation buildscript { repositories { jcenter() } dependencies { classpath

    'com.android.tools.build:gradle:1.2.3' classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6' } }
  10. Installation buildscript { repositories { jcenter() } dependencies { classpath

    'com.android.tools.build:gradle:1.2.3' classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6' } }
  11. Installation buildscript { repositories { jcenter() } dependencies { classpath

    'com.android.tools.build:gradle:1.2.3' classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6' } } gradle-groovy-android-plugin 0.3.6 has not supported android gradle plugin 1.3 higher... 
  12. GreeterSpec with Spock class GreeterSpec extends Specification { def "#say()"()

    { expect: expected == new Greeter().say(period) where: period | expected "morning" | "Good morning" "afternoon" | "Hello" "night" | "Good night" } }
  13. GreeterSpec with Spock class GreeterSpec extends Specification { def "#say()"()

    { expect: expected == new Greeter().say(period) where: period | expected "morning" | "Good morning" "afternoon" | "Hello" "night" | "Good night" } }
  14. GreeterSpec with Spock class GreeterSpec extends Specification { def "#say()"()

    { expect: expected == new Greeter().say(period) where: period | expected "morning" | "Good morning" "afternoon" | "Hello" "night" | "Good night" } }