robospock droidcon '14

28
Applause www.applause.com Crowd Testing Beta Distributions Crash Reports Application Analytics We’re hiring!

Upload: przemyslaw-jakubczyk

Post on 15-Aug-2015

25 views

Category:

Software


1 download

TRANSCRIPT

Applause

www.applause.com

❏ Crowd Testing❏ Beta Distributions❏ Crash Reports❏ Application Analytics

❏ We’re hiring!

Przemek JakubczykSenior Android Developer

Applause [email protected]

GH: pjakubczyk

Robospock

def "should change label on button click"(){

given:

def button = new Button()

when:

button.click()

then:

button.getLabel() == "The new label"

}

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

}

def "should throw RuntimeException on button click"(){

given:

def button = new Button()

when:

button.click()

then:

thrown(RuntimeException)

}

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

}

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(_)

}

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

}

def "should return first element from list"(){

given:

def list = Mock(List) {

get(0) >> "First"

}

expect:

list.get(0) == "First"

}

@Test

public void shouldReturnFirstElementFromList() {

// given

List<String> 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"

}

def "maximum of two numbers"() {

expect:

Math.max(first, second) == result

where:

first | second | result

3 | 5 | 5

7 | 0 | 7

0 | 0 | 0

}

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

};

}

def "should change label on button click"(){

given: "create new button"

when: "perform the click action"

then: "the label has changed"

}

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"

}

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

It was Spock and Groovy

Where is Android?

Robolectric + Spock = RoboSpock

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"

}

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]", _)

}

Why it is worth to try

● Groovy is colorful and funny● Strict test format given/when/then

● Human readable as hell● BDD

You are not losing anything

Robolectric

Based on jUnit ● Full support with Java tools like jacoco● XML/HTML reports for CI

How to start?

Make it run with gradleWhat is my first testWhat should I test

Huston, we have a problem

● Robolectric configuration is not easy● RoboSpock as well● https://github.com/pelotasplus/RoboSpock-

tutorial

What’s next

Site to update:www.robospock.org

Report issue:https://github.com/robospock

QA

Przemek JakubczykSenior Android Developer

Applause [email protected]

GH: pjakubczyk

Applause

We are hiring!Please send your referrals to:

[email protected]