spock - the next stage of unit testing
TRANSCRIPT
![Page 1: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/1.jpg)
SpockThe next state of Unit testing
![Page 2: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/2.jpg)
About me
![Page 3: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/3.jpg)
About me
![Page 4: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/4.jpg)
About me
5+ years
![Page 5: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/5.jpg)
About me
5+ years
![Page 6: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/6.jpg)
Disclaimer
![Page 7: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/7.jpg)
Agenda
![Page 8: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/8.jpg)
Agenda● Unit testing
![Page 9: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/9.jpg)
Agenda● Unit testing● What is Spock
![Page 10: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/10.jpg)
Agenda● Unit testing● What is Spock● Why
![Page 11: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/11.jpg)
Agenda● Unit testing● What is Spock● Why● Demo
![Page 12: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/12.jpg)
Agenda● Unit testing● What is Spock● Why● Demo● Q&A
![Page 13: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/13.jpg)
Unit Testing
![Page 14: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/14.jpg)
Main thoughts
![Page 15: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/15.jpg)
Main thoughts● Проверяем соответствие требованиям
![Page 16: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/16.jpg)
Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно
![Page 17: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/17.jpg)
Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно● Находим проблемы на раннем этапе
![Page 18: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/18.jpg)
Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно● Находим проблемы на раннем этапе● Проверяем, что ничего не сломали при
изменении
![Page 19: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/19.jpg)
Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно● Находим проблемы на раннем этапе● Проверяем, что ничего не сломали при
изменении● Строим дизайн системы
![Page 20: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/20.jpg)
Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно● Находим проблемы на раннем этапе● Проверяем, что ничего не сломали при
изменении● Строим дизайн системы● Документируем поведение системы
![Page 21: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/21.jpg)
Typical Java Unit test
![Page 22: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/22.jpg)
Typical Java Unit testpublic class JUnitClassicStateResolverSpec {
@Test public void classicStatesShouldUseGameRules() {
StateResolver scissorsResolver = new ScissorsStateResolver(); StateResolver rockResolver = new RockStateResolver(); StateResolver paperResolver = new PaperStateResolver();
assertEquals(false, scissorsResolver.tryToBeat(State.SPOCK)); assertEquals(true, scissorsResolver.tryToBeat(State.LIZARD)); assertEquals(true, scissorsResolver.tryToBeat(State.OUT)); assertEquals(true, scissorsResolver.tryToBeat(State.PAPER)); assertEquals(false, scissorsResolver.tryToBeat(State.ROCK)); assertEquals(false, scissorsResolver.tryToBeat(State.SCISSORS));
assertEquals(false, rockResolver.tryToBeat(State.SPOCK)); assertEquals(false, rockResolver.tryToBeat(State.LIZARD)); assertEquals(true, rockResolver.tryToBeat(State.OUT)); assertEquals(false, rockResolver.tryToBeat(State.PAPER)); assertEquals(false, rockResolver.tryToBeat(State.ROCK)); assertEquals(true, rockResolver.tryToBeat(State.SCISSORS));
assertEquals(true, paperResolver.tryToBeat(State.SPOCK)); assertEquals(false, paperResolver.tryToBeat(State.LIZARD)); assertEquals(true, paperResolver.tryToBeat(State.OUT)); assertEquals(false, paperResolver.tryToBeat(State.PAPER)); assertEquals(true, paperResolver.tryToBeat(State.ROCK)); assertEquals(false, paperResolver.tryToBeat(State.SCISSORS));
}}
![Page 23: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/23.jpg)
Typical Java Unit testclassicStatesShouldUseGameRules(net.astanajug.meetup.spock.JUnitClassicStateResolverSpec) Time elapsed: 0.003 sec <<< FAILURE!java.lang.AssertionError: expected:<false> but was:<true> at org.junit.Assert.fail(Assert.java:88) at org.junit.Assert.failNotEquals(Assert.java:834) at org.junit.Assert.assertEquals(Assert.java:118) at org.junit.Assert.assertEquals(Assert.java:144) at net.astanajug.meetup.spock.JUnitClassicStateResolverSpec.classicStatesShouldUseGameRules(JUnitClassicStateResolverSpec.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)+100500 stack trace lines at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350) at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)+100500 stack trace lines
![Page 24: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/24.jpg)
![Page 25: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/25.jpg)
Spock
![Page 26: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/26.jpg)
What is?
![Page 27: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/27.jpg)
What is?● Начат в 2008 году
![Page 28: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/28.jpg)
What is?● Начат в 2008 году
● Создан Peter Niederweiser и поддерживается Luke Daley и сообществом
![Page 29: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/29.jpg)
What is?● Начат в 2008 году
● Создан Peter Niederweiser и поддерживается Luke Daley и сообществом
● Разработан с использованием Groovy
![Page 30: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/30.jpg)
What is?● Начат в 2008 году
● Создан Peter Niederweiser и поддерживается Luke Daley и сообществом
● Разработан с использованием Groovy
● Базируется на идеях JUnit, jMock, Mockito, RSpec, JUnitParameters и Start Trek
![Page 31: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/31.jpg)
What is?● Начат в 2008 году
● Создан Peter Niederweiser и поддерживается Luke Daley и сообществом
● Разработан с использованием Groovy
● Базируется на идеях JUnit, jMock, Mockito, RSpec, JUnitParameters и Start Trek
● Первый “мажорный” релиз выпущен в 2015 году
![Page 32: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/32.jpg)
Why?
![Page 33: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/33.jpg)
Why?●Совместим с JUnit
![Page 34: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/34.jpg)
Why?●Совместим с JUnit
●Легко попробовать - http://webconsole.spockframework.org
![Page 35: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/35.jpg)
Why?●Совместим с JUnit
●Легко попробовать - http://webconsole.spockframework.org
●“Выразительная” конструкция построения теста - Given-When-Then
![Page 36: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/36.jpg)
Given-When-Then exampledef "A basket with one product has equal weight"() { given: "an empty basket and a TV" Product tv = new Product(name:"bravia",price:1200,weight:18) Basket basket = new Basket() when: "user wants to buy the TV" basket.addProduct(tv) then: "basket weight is equal to the TV" basket.currentWeight == tv.weight }
![Page 37: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/37.jpg)
Why?●Совместим с JUnit
●Легко попробовать - http://webconsole.spockframework.org
●“Выразительная” конструкция построения теста - Given-When-Then
●Понятные сообщения об ошибках
![Page 38: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/38.jpg)
Informative message exampleimport spock.lang.*
class MyFirstSpec extends Specification { def "let's try this!"() { expect: Math.max(1, 2) == 3 }}
MyFirstSpec - let's try this! FAILED Condition not satisfied: Math.max(1, 2) == 3 | | 2 false at MyFirstSpec.let's try this!(Script1.groovy:7)
![Page 39: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/39.jpg)
Why?●Совместим с JUnit
●Легко попробовать - http://webconsole.spockframework.org
●“Выразительная” конструкция построения теста - Given-When-Then
●Понятные сообщения об ошибках
●Возможность тестирования взаимодействий через моки
![Page 40: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/40.jpg)
Interaction testing exampledef "credit card connection is always closed down"() {
given: "a basket, a customer and a TV"Product tv = new Product(name:"bravia",price:1200,weight:18)BillableBasket basket = new BillableBasket()Customer customer = new Customer(...)
and: "a credit card service"CreditCardProcessor creditCardSevice = Mock(CreditCardProcessor)basket.setCreditCardProcessor(creditCardSevice)
when: "user checks out the tv"basket.addProduct tvbasket.checkout(customer)
then: "connection is always closed at the end"1 * creditCardSevice.shutdown()
}
![Page 41: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/41.jpg)
Why?●Совместим с JUnit
●Легко попробовать - http://webconsole.spockframework.org
●“Выразительная” конструкция построения теста - Given-When-Then
●Понятные сообщения об ошибках
●Возможность тестирования взаимодействий через моки
●Простота тестирования с использованием наборов данных
![Page 42: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/42.jpg)
Data Driven exampleclass HelloSpock extends spock.lang.Specification { def "length of Spock's and his friends' names"() { expect: name.size() == length
where: name | length "Spock" | 5 "Kirk" | 4 "Scotty" | 6 }}
![Page 43: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/43.jpg)
Demo
![Page 44: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/44.jpg)
![Page 45: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/45.jpg)
Rules
![Page 46: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/46.jpg)
![Page 47: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/47.jpg)
Resources
![Page 48: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/48.jpg)
Resource●http://spockframework.org
●http://webconsole.spockframework.org
●Java Testing with Spock by Konstantinos Kapelonis (https://manning.com/books/java-testing-with-spock)
![Page 49: Spock - the next stage of unit testing](https://reader035.vdocuments.site/reader035/viewer/2022070601/5899ba1f1a28aba11e8b6d01/html5/thumbnails/49.jpg)
Questions & Answers