Spock-Framework: The Logical Choice for Modern Testing
27. Mar 2023•0 gefällt mir
0 gefällt mir
Sei der Erste, dem dies gefällt
Mehr anzeigen
•6 Aufrufe
Aufrufe
Aufrufe insgesamt
0
Auf Slideshare
0
Aus Einbettungen
0
Anzahl der Einbettungen
0
Downloaden Sie, um offline zu lesen
Melden
Software
when you are a developer, adding test cases to code is good practice and it helps in the debugging process but writing and understanding test cases are hard.
Spock-Framework: The Logical Choice for Modern Testing
Spock-Framework: The Logical Choice
for Modern Testing
Introduction
We'll look at Spock, a Groovy testing framework. Mainly, Spock aims to be a more powerful
alternative to the traditional JUnit stack, by leveraging Groovy features.
By making use of Groovy, Spock introduces new and expressive ways of testing our Java
applications, which simply aren't possible in ordinary Java code. We'll explore some of Spock's
high-level concepts during this article, with some practical step-by-step examples.
Prerequisite
You have some knowledge about spring-boot and testing concept like mocking, Stubbing, etc.
Here we learn the Spock framework with the Spring boot application.
To use Spock in spring boot, add the below dependency into pom.xml
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.3-groovy-4.0</version>
<scope>test</scope>
@SpringBootTest
class Test1 extends Specification {
def "Simple Test"(){
expect:
1+1 == 2
}
}
Here “def” keyword is used to define method after this you can add method discerption.
Now run the test
Output:
You see Test Case Passed successfully.
Controller Test-Cases
First, create a ‘Hello’ controller
@RestController
public class Hello {
@GetMapping("/hello")
public String greeting (){
return "Hello World! ";
}
Now for this controller, we are writing test cases using Spock.
class HelloTest extends Specification {
@MockBean
def cont = new Hello ()
MockMvc mvc = MockMvcBuilders.standaloneSetup(cont). build()
def "when get is performed then the response has status 200 and content is 'Hello
world!'"() {
expect: "Status is 200 and the response is 'Hello world!'"
mvc.perform(MockMvcRequestBuilders.get("/hello"))
.andExpect(status().isOk())
.andReturn()
.response
.contentAsString == "Hello World! "
}
}
Output:
Keywords:
1. Expect: This is a way of performing our stimulus and assertion within the same block.
Depending on what we find more expressive, we may or may not choose to use this
block
2. When – This is where we provide a stimulus to what is under test. In other words, where
we invoke our method under test
3. Then – This is where the assertions belong. In Spock, these are evaluated as plain
Boolean assertions, which will be covered later
Important Tips
1) All the blocks in a Spock based spec are optional. However, if present “when” and “then”
should appear together in sequence.
2) If you just need to validate an assertion (and you don’t have a need of when block), you can
use “expect” block. It can be used to assert pre-conditions even before when and then blocks
(and can be used multiple times in a test).
Originally published by: Spock Framework