The document discusses testing with Spock, a Groovy-based testing framework. It provides examples of different Spock features like BDD-style tests using Given-When-Then, data-driven testing with the @Unroll annotation, mocking, and sharing test state between specifications using the @Shared annotation or setupSpec method. It also covers Spock's integration with JUnit and how to write Spock tests that are compatible with both JUnit 4 and JUnit 5.
9. def “files added to a bucket are available in the bucket”() {
given:
def filePath = Paths.get("src/test/resources/car.jpg")
InputStream stream = Files.newInputStream(filePath)
when:
fileStorageService.storeFile(TARGET_BUCKET, "vehicles/123.jpg", stream)
then:
amazonS3Service.exists(TARGET_BUCKET, "vehicles/123.jpg")
}
12. void "list assertion example"() {
given:
List<Integer> numbers = [1, 2, 3]
when:
/
/
List<Integer> result = numbers.collect { it * 2}
List<Integer> result = [2, 2, 6]
then:
result
=
=
[2, 4, 6]
}
Condition not satis
fi
ed:
result == [2, 4, 6]
| |
| false
[2, 2, 6]
13. void "list contains expected element"() {
expect:
10 in [2, 4, 6]
}
Condition not satis
fi
ed:
10 in [2, 4, 6]
|
false
14. void "assert all"() {
expect:
verifyAll {
10 in [2, 4, 6]
12 in [2, 4, 6]
}
}
Multiple Failures (2 failures)
org.spockframework.runtime.ConditionNotSatis
fi
edError: Condition not satis
fi
ed:
10 in [2, 4, 6]
|
false
org.spockframework.runtime.ConditionNotSatis
fi
edError: Condition not satis
fi
ed:
12 in [2, 4, 6]
|
false
18. @Unroll
void "#a + #b should be #expectedSum"() {
when:
def sum = a + b
then:
sum
=
=
expectedSum
where:
a | b | expectedSum
10 | 10 | 20
20 | 20 | 40
}
28. public class SpockEngine extends HierarchicalTestEngine<SpockExecutionContext> {
@Override
public String getId() {
return "spock";
}
/
/
more code
}
30. public class SampleTest {
private StateHolder stateHolder = new StateHolder();
@Test
void test1() {
/
/
use stateHolder
}
@Test
void test2() {
/
/
use stateHodler
}
}
public class StateHolder {
public StateHolder() {
System.out.println("Instantiating StateHolder");
}
}
31. public class SampleTest {
private StateHolder stateHolder = new StateHolder();
@Test
void test1() {
/
/
use stateHolder
}
@Test
void test2() {
/
/
use stateHodler
}
}
public class StateHolder {
public StateHolder() {
System.out.println("Instantiating StateHolder");
}
}
@BeforeEach
private void setup() {
stateHolder = new StateHolder();
}
32. public class SampleSharedStateTest {
private StateHolder stateHolder;
@BeforeAll
void beforeAll() {
stateHolder = new StateHolder();
}
@Test
void test1() {
/
/
use shared stateHolder
System.out.println(stateHolder);
}
@Test
void test2() {
/
/
use shared stateHodler
System.out.println(stateHolder);
}
}
33. public class SampleSharedStateTest {
private StateHolder stateHolder;
@BeforeAll
void beforeAll() {
stateHolder = new StateHolder();
}
@Test
void test1() {
/
/
use shared stateHolder
System.out.println(stateHolder);
}
@Test
void test2() {
/
/
use shared stateHodler
System.out.println(stateHolder);
}
}
@BeforeAll method 'void
com.nareshak.demo.SampleSharedStateTest.beforeAll()'
must be static unless the test class is annotated with
@TestInstance(Lifecycle.PER_CLASS).
34. public class SampleSharedStateTest {
private static StateHolder stateHolder;
@BeforeAll
static void beforeAll() {
stateHolder = new StateHolder();
}
@Test
void test1() {
/
/
use shared stateHolder
System.out.println(stateHolder);
}
@Test
void test2() {
/
/
use shared stateHodler
System.out.println(stateHolder);
}
}
Instantiating StateHolder
com.nareshak.demo.StateHolder@5bea6e0
com.nareshak.demo.StateHolder@5bea6e0