Slide 1

Slide 1 text

Groovy/Spock for Android app testing Masayuki IZUMI - @izumin5210 potatotips #22

Slide 2

Slide 2 text

 > Masayuki IZUMI a.k.a. @izumin5210 > Rekimoto Lab. at the University of Tokyo > Wantedly, Inc. > Rubyist / Androider / {Java,Type}Scripter

Slide 3

Slide 3 text

Ruby JavaScript Android Design

Slide 4

Slide 4 text

Parameterized test with JUnit

Slide 5

Slide 5 text

public class GreeterTest { @Test public void sayHello() throws Exception { Greeter greeter = new Greeter(); assertEquals("Hello", greeter.say()); } }

Slide 6

Slide 6 text

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")); } }

Slide 7

Slide 7 text

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")); } }

Slide 8

Slide 8 text

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])); } } }

Slide 9

Slide 9 text

@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())); } }

Slide 10

Slide 10 text

Parameterized test with JUnit  Too complecated and verbose  Stop execution when assertion failed - run -> fail -> fix -> run -> fail -> fix -> run -> fail ...

Slide 11

Slide 11 text

Spock “The Enterprise-ready testing and specification framework.” https://github.com/spockframework/spock https://www.flickr.com/photos/124561666@N02/14218023847

Slide 12

Slide 12 text

Spock  Testing framework written in Groovy  Inspired from JUnit, RSpec, Mockito, ...  Very fluent specification DSL (like RSpec?)  Descriptive assertion message (Power Assert!!!)

Slide 13

Slide 13 text

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' } }

Slide 14

Slide 14 text

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' } }

Slide 15

Slide 15 text

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... 

Slide 16

Slide 16 text

Installation dependencies { // ... testCompile "org.spockframework:spock-core:1.0-groovy-2.4" }

Slide 17

Slide 17 text

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" } }

Slide 18

Slide 18 text

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" } }

Slide 19

Slide 19 text

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" } }

Slide 20

Slide 20 text

Assertion message

Slide 21

Slide 21 text

Assertion message

Slide 22

Slide 22 text

JUnit v.s. Spock

Slide 23

Slide 23 text

https://speakerdeck.com/jnchito/number-kanrk06

Slide 24

Slide 24 text

Groovy/Spock for Android app testing Masayuki IZUMI - @izumin5210 potatotips #22