13. Conclude
given:
I am testing Android
when:
I need easy tool
then:
I choose
RoboSpock
Robolectric + SpockRoboSpock
14. “to conclue”
One Robospock to rule them all
One Robospock to find them
One Robospock to bring them all and in the darkness bind them
15. Robospock
One Robospock to rule them all
One Robospock to find them
One Robospock to bring them all and in the darkness bind them
16. THEN I have organized syntax
benefits from
def “should update first letter” {
given:
def myLabel = new MyLabel(“adam”)
when:
myLabel.transform()
then:
myLabel.userName == “Adam”
}
Spock
Groovy
17. 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
18. AND
benefits from Spock
given:
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
19. AND
benefits from Spock
given:
def myLabel = new Label()
when:
myLabel.setColor(color)
then:
myLabel.getColor() == color
where:
color << [Color.RED, Color.WHITE, Color.BLUE]
I can easily check many conditions
20. AND
benefits from Spock
given:
def myLabel = new MyLabel()
when:
myLabel.setColor(color)
then:
myLabel.getColor() == color
where:
color << [Color.RED, Color.WHITE, Color.BLUE]
I can easily check many conditions
21. AND
benefits from Spock
given:
def myLabel = new Label()
when:
myLabel.setName(“James Bond”)
then:
thrown(FictionalCharacterNameException)
I can organize exceptions
22. 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
23. 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
24. 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
25. AND
given:
def activity.sharedPref = Mock(SharedPreferences) {
getString(“metJamesBond”) >> false
}
when:
activity.myButton.performClick()
then:
activity.myLabel.getText() == “Sorry, you
haven’t meet James”
Benefits from Groovy
I can use closures in Mocks
26. 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
27. AND
Benefits from Robolectric
I can use helpful shadows
given:
def myButton = new Button(...)
when:
myButton.performClick()
then:
ShadowToast.lastToast
ShadowToast.textOfLastToast
== “I <3 Minsk and James Bond too”
28. 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
35. Tips:
1. Try to create an interface between your code and OS code
a) Toasts
b) starting activity/service
2. Make a barrier from 3rd party code
for example inherit from RoboSpecfication
Because author might change package name :)