spock - the next stage of unit testing

49
Spock The next state of Unit testing

Upload: jugkaraganda

Post on 13-Apr-2017

122 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Spock - the next stage of unit testing

SpockThe next state of Unit testing

Page 2: Spock - the next stage of unit testing

About me

Page 3: Spock - the next stage of unit testing

About me

Page 4: Spock - the next stage of unit testing

About me

5+ years

Page 5: Spock - the next stage of unit testing

About me

5+ years

Page 6: Spock - the next stage of unit testing

Disclaimer

Page 7: Spock - the next stage of unit testing

Agenda

Page 8: Spock - the next stage of unit testing

Agenda● Unit testing

Page 9: Spock - the next stage of unit testing

Agenda● Unit testing● What is Spock

Page 10: Spock - the next stage of unit testing

Agenda● Unit testing● What is Spock● Why

Page 11: Spock - the next stage of unit testing

Agenda● Unit testing● What is Spock● Why● Demo

Page 12: Spock - the next stage of unit testing

Agenda● Unit testing● What is Spock● Why● Demo● Q&A

Page 13: Spock - the next stage of unit testing

Unit Testing

Page 14: Spock - the next stage of unit testing

Main thoughts

Page 15: Spock - the next stage of unit testing

Main thoughts● Проверяем соответствие требованиям

Page 16: Spock - the next stage of unit testing

Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно

Page 17: Spock - the next stage of unit testing

Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно● Находим проблемы на раннем этапе

Page 18: Spock - the next stage of unit testing

Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно● Находим проблемы на раннем этапе● Проверяем, что ничего не сломали при

изменении

Page 19: Spock - the next stage of unit testing

Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно● Находим проблемы на раннем этапе● Проверяем, что ничего не сломали при

изменении● Строим дизайн системы

Page 20: Spock - the next stage of unit testing

Main thoughts● Проверяем соответствие требованиям● Делаем быстро и точечно● Находим проблемы на раннем этапе● Проверяем, что ничего не сломали при

изменении● Строим дизайн системы● Документируем поведение системы

Page 21: Spock - the next stage of unit testing

Typical Java Unit test

Page 22: Spock - the next stage of unit testing

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

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
Page 25: Spock - the next stage of unit testing

Spock

Page 26: Spock - the next stage of unit testing

What is?

Page 27: Spock - the next stage of unit testing

What is?● Начат в 2008 году

Page 28: Spock - the next stage of unit testing

What is?● Начат в 2008 году

● Создан Peter Niederweiser и поддерживается Luke Daley и сообществом

Page 29: Spock - the next stage of unit testing

What is?● Начат в 2008 году

● Создан Peter Niederweiser и поддерживается Luke Daley и сообществом

● Разработан с использованием Groovy

Page 30: Spock - the next stage of unit testing

What is?● Начат в 2008 году

● Создан Peter Niederweiser и поддерживается Luke Daley и сообществом

● Разработан с использованием Groovy

● Базируется на идеях JUnit, jMock, Mockito, RSpec, JUnitParameters и Start Trek

Page 31: Spock - the next stage of unit testing

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

Why?

Page 33: Spock - the next stage of unit testing

Why?●Совместим с JUnit

Page 34: Spock - the next stage of unit testing

Why?●Совместим с JUnit

●Легко попробовать - http://webconsole.spockframework.org

Page 35: Spock - the next stage of unit testing

Why?●Совместим с JUnit

●Легко попробовать - http://webconsole.spockframework.org

●“Выразительная” конструкция построения теста - Given-When-Then

Page 36: Spock - the next stage of unit testing

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

Why?●Совместим с JUnit

●Легко попробовать - http://webconsole.spockframework.org

●“Выразительная” конструкция построения теста - Given-When-Then

●Понятные сообщения об ошибках

Page 38: Spock - the next stage of unit testing

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

Why?●Совместим с JUnit

●Легко попробовать - http://webconsole.spockframework.org

●“Выразительная” конструкция построения теста - Given-When-Then

●Понятные сообщения об ошибках

●Возможность тестирования взаимодействий через моки

Page 40: Spock - the next stage of unit testing

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

Why?●Совместим с JUnit

●Легко попробовать - http://webconsole.spockframework.org

●“Выразительная” конструкция построения теста - Given-When-Then

●Понятные сообщения об ошибках

●Возможность тестирования взаимодействий через моки

●Простота тестирования с использованием наборов данных

Page 42: Spock - the next stage of unit testing

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

Demo

Page 44: Spock - the next stage of unit testing
Page 45: Spock - the next stage of unit testing

Rules

Page 46: Spock - the next stage of unit testing
Page 47: Spock - the next stage of unit testing

Resources

Page 48: Spock - the next stage of unit testing

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

Questions & Answers