RoboSpock Poznań ADG 2016

Post on 11-Apr-2017

279 views 2 download

Transcript of RoboSpock Poznań ADG 2016

pjakubczyk

Przemek JakubczykAndroid Technical Lead @Applause @pjakubczy

k

RoboSpock.orgrobospock

RoboSpockenhance Android test with Groovy

Introductiongiven:

I have lots of testing tools

expect:

I want better unit test

GIVEN

JVMjUnit

Assert

Mockito

TestNG

AssertJ

PowerMock

GIVEN

JVMjUnit

Assert

Mockito

TestNG

AssertJ

PowerMock

GIVEN

JVMjUnit

Assert

Mockito

TestNG

AssertJ

PowerMock

GIVEN

JVMjUnit

Assert

Mockito

TestNG

AssertJ

PowerMock

GIVEN

AndroidAndroidJUnitRunner

Espresso

Robolectric

Robotium

GIVEN

AndroidJUnitRunner

Espresso

Robolectric

Robotium

JVM vs Dalvik Androi

d

GIVEN

AndroidJUnitRunner

Espresso

Robolectric

Robotium

UI testingAndroi

d

JVMwhen:

I work using regular JVM

then:

I choose Spock

Androidwhen:

I am working with Android

then:

I choose Robolectric

Concludegiven:

I am testing Android

when:

I need easy tool

then:

I choose

RoboSpock

Robolectric + SpockRoboSpock

RobospockOne Robospock to rule them all

One Robospock to find them

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

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

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

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

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

AND benefits from Spock

given:def myLabel = new Label()

when:myLabel.setName(“James Bond”)

then:thrown(FictionalCharacterNameException)

I can organize exceptions

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

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

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

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

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

AND Benefits from Robolectric

I can use helpful shadows

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

when:myButton.performClick()

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

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

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

Android Studio

Android Studio

Thanks. Any Questions?

Przemek Jakubczyk

Android Technical Lead @Applause

gh: pjakubczyk

@pjakubczyk

www.robospock.org