void "Eine Liste ist initial leer"() {
expect:
new ArrayList<>().isEmpty() == true
}
Silvio Wangler
Entwickler mit Java, Groovy, Spring Hintergrund
38 Jahre alt
lebt in Opfikon ZH, geboren und aufgewachsen in Zürich-Albisrieden
Ehemann und Vater
Ein Testing Framework für Java Applikationen
Inspiriert durch JUnit, Mockito, jMock, Groovy, Scala
Es lässt einem Spezifikationen für eine bestimmte Klasse oder eine ganze Applikation schreiben
Es lässt Tests, durch den Einsatz einer Domain Specific Language, lesbarer werden
void "Eine Liste ist initial leer"() {
expect:
new ArrayList<>().isEmpty() == true
}
Spock Spezifikationen werden in Groovy Klassen implementiert.
package ch.silviowangler.spock.intro
import spock.lang.Specification
class SimpleSpockWithoutCommentsSpec extends Specification {
void "Eine Liste ist initial leer"() {
expect:
new ArrayList<>().isEmpty() == true
}
}
3! = 3 * 2 * 1 = 6
@Unroll
void "Die Fakultät von #number ist #result"()
{
expect: 'Die Fakultät wird korrekt berechntet'
Factorial.of(number) == result
where: 'Datentabelle'
number || result
0L || 1L
1L || 1L
2L || 2L
3L || 6L
}
Was bedeuten die Blöcke given
, expect
, when
, usw. in Spock?
package ch.silviowangler.spock.booking;
/**
* @author Silvio Wangler
*/
public class BookingService {
// teure Ressource, wollen wir im Test besser mocken
private BookingBackend bookingBackend;
public BookingService(BookingBackend bookingBackend) {
this.bookingBackend = bookingBackend;
}
public boolean bookEvent(String eventId, int amount) {
try {
this.bookingBackend.bookEvent(eventId, amount);
return true;
} catch (BookingException e) {
return false;
}
}
}
void "Buche 2 Ticket für eine Veranstaltung"(){
given:
def backend = Mock(BookingBackend)
BookingService bookingService = new BookingService(backend)
when: 'zwei Tickets für die Veranstalung 22 gebucht werden'
def result = bookingService.bookEvent('22', 2)
then: 'ist die Transaktion erfolgreich'
result
and:
1 * backend.bookEvent('22', 2)
0 * _
}
void "Simuliere Fehler beim Buchen"(){
given:
def backend = Mock(BookingBackend)
BookingService bookingService = new BookingService(backend)
def ex = new BookingException()
when: 'zwei Tickets für die Veranstalung 22 gebucht werden'
def result = bookingService.bookEvent('22', 2)
then: 'ist die Transaktion nicht erfolgreich'
!result
and:
1 * backend.bookEvent('22', 2) >> { throw ex }
0 * _
}
Diese Präsentation ist unter https://saw303.github.io/spock-intro/ publiziert.
Der Source Code findet sich auf https://github.com/saw303/spock-intro
Mit Spock spielen kann man in der Spock Web Konsole unter http://meetspock.appspot.com/.
Die Spock Dokumentation findet man unter http://spockframework.org/spock/docs/
Vielen Dank für Ihre Aufmerksamkeit.