RoboSpock Poznań ADG 2016

30
pjakubczy k Przemek Jakubczyk Android Technical Lead @Applause @pjakubczy k RoboSpock.or g robospoc k

Transcript of RoboSpock Poznań ADG 2016

Page 1: RoboSpock Poznań ADG 2016

pjakubczyk

Przemek JakubczykAndroid Technical Lead @Applause @pjakubczy

k

RoboSpock.orgrobospock

Page 2: RoboSpock Poznań ADG 2016

RoboSpockenhance Android test with Groovy

Page 3: RoboSpock Poznań ADG 2016

Introductiongiven:

I have lots of testing tools

expect:

I want better unit test

Page 4: RoboSpock Poznań ADG 2016

GIVEN

JVMjUnit

Assert

Mockito

TestNG

AssertJ

PowerMock

Page 5: RoboSpock Poznań ADG 2016

GIVEN

JVMjUnit

Assert

Mockito

TestNG

AssertJ

PowerMock

Page 6: RoboSpock Poznań ADG 2016

GIVEN

JVMjUnit

Assert

Mockito

TestNG

AssertJ

PowerMock

Page 7: RoboSpock Poznań ADG 2016

GIVEN

JVMjUnit

Assert

Mockito

TestNG

AssertJ

PowerMock

Page 8: RoboSpock Poznań ADG 2016

GIVEN

AndroidAndroidJUnitRunner

Espresso

Robolectric

Robotium

Page 9: RoboSpock Poznań ADG 2016

GIVEN

AndroidJUnitRunner

Espresso

Robolectric

Robotium

JVM vs Dalvik Androi

d

Page 10: RoboSpock Poznań ADG 2016

GIVEN

AndroidJUnitRunner

Espresso

Robolectric

Robotium

UI testingAndroi

d

Page 11: RoboSpock Poznań ADG 2016

JVMwhen:

I work using regular JVM

then:

I choose Spock

Page 12: RoboSpock Poznań ADG 2016

Androidwhen:

I am working with Android

then:

I choose Robolectric

Page 13: RoboSpock Poznań ADG 2016

Concludegiven:

I am testing Android

when:

I need easy tool

then:

I choose

RoboSpock

Robolectric + SpockRoboSpock

Page 14: RoboSpock Poznań ADG 2016

RobospockOne Robospock to rule them all

One Robospock to find them

One Robospock to bring them all and in the darkness bind them

Page 15: RoboSpock Poznań ADG 2016

THENI have organized syntax

benefits from

def “should update first letter” {given:def myLabel = new MyLabel(“adam”)

when:myLabel.transform()

then:myLabel.userName == “Adam”}

SpockGroovy

Page 16: RoboSpock Poznań ADG 2016

AND

def “should start new activity”

def “should change color when action occurred”

def “should return always top element from the stack”

def “should I care about how long test name really can be?”

benefits from Groovy

I can use human readable method names

Page 17: RoboSpock Poznań ADG 2016

AND benefits from Spockgiven:

def myButton = new Button(...)

and:def listener = Mock(OnClickListener)myButton.setOnClickListener(listener)when:myButton.performClick()

then:1 * listener.onClick(myButton)

I can use powerful Mocks

Page 18: RoboSpock Poznań ADG 2016

AND benefits from Spock

given:def myLabel = new Label()

when:myLabel.setColor(color)then:myLabel.getColor() == colorwhere:color << [Color.RED, Color.WHITE, Color.BLUE]

I can easily check many conditions

Page 19: RoboSpock Poznań ADG 2016

AND benefits from Spock

given:def myLabel = new Label()

when:myLabel.setName(“James Bond”)

then:thrown(FictionalCharacterNameException)

I can organize exceptions

Page 20: RoboSpock Poznań ADG 2016

And many more (>10) available on

http://spockframework.github.io/spock/docs/1.0/extensions.html

AND benefits from Spock

@Stepwise

@Issue("http://my.issues.org/FOO-1")

@IgnoreIf(reason == "TODO")

@Timeout(5)

I can use other Spock extensions

Page 21: RoboSpock Poznań ADG 2016

AND benefits from Groovy

given:def dao = new UserDao()

and:def users = (1..10).collect{ User.newInstance() }then:dao.insert(users)

where:dao.getUserCount() == 10

I can use groovy collections

Page 22: RoboSpock Poznań ADG 2016

AND benefits from Groovy

given:def users = (1..10).collect{ User.newInstance() }

when:def result = users.find { it.name == “James” }then:result.lastName == “Bond”

I can use groovy collections

Page 23: RoboSpock Poznań ADG 2016

ANDgiven:

def activity.sharedPref = Mock(SharedPreferences) { getString(“metJamesBond”) >> false}when:

activity.myButton.performClick()then:

activity.myLabel.getText() == “Sorry, you haven’t met James”

Benefits from Groovy

I can use closures in Mocks

Page 24: RoboSpock Poznań ADG 2016

THEN Benefits from Robolectric

given:def myLabel = new TextView(RuntimeEnvironment.application)

when:myLabel.setText(“James Bond”)

then:myLabel.getText() == “James Bond”

I can use Context

Page 25: RoboSpock Poznań ADG 2016

AND Benefits from Robolectric

I can use helpful shadows

given:def myButton = new Button(...)

when:myButton.performClick()

then:ShadowToast.lastToastShadowToast.textOfLastToast == “James Bond”

Page 26: RoboSpock Poznań ADG 2016

AND Benefits from Robolectric

I can use helpful shadows

given:def myButton = new Button(...)

and:def listener = new View.OnClickListener() {...}when:myButton.setOnClickListener(listener)then:Shadows.shadowOf(myButton) .getOnClickListener() == listener

Page 27: RoboSpock Poznań ADG 2016

buildscript {

dependencies {

classpath 'com.android.tools.build:gradle:1.3.0'

classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6' }

}

apply plugin: 'com.android.application'

apply plugin: 'groovyx.grooid.groovy-android'

dependencies {

testCompile 'org.robospock:robospock:1.0.0'}

build.gradle

Page 28: RoboSpock Poznań ADG 2016

Android Studio

Page 29: RoboSpock Poznań ADG 2016

Android Studio

Page 30: RoboSpock Poznań ADG 2016

Thanks. Any Questions?

Przemek Jakubczyk

Android Technical Lead @Applause

gh: pjakubczyk

@pjakubczyk

www.robospock.org