Spock (#5952)
* BAEL-2364: Adds Spring Boot IntegrationTests with Spock Framework * Adds o.b.boot.controller.rest.WebController as example controller * Adds one Load Application Context Test * Adds one WebMvcTest * updated pom.xml's and README * fixed module name
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package org.baeldung.boot
|
||||
|
||||
import org.baeldung.boot.controller.rest.WebController
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import spock.lang.Narrative
|
||||
import spock.lang.Specification
|
||||
import spock.lang.Title
|
||||
|
||||
@Title("Application Specification")
|
||||
@Narrative("Specification which beans are expected")
|
||||
@SpringBootTest
|
||||
class LoadContextTest extends Specification {
|
||||
|
||||
@Autowired(required = false)
|
||||
private WebController webController
|
||||
|
||||
|
||||
def "when context is loaded then all expected beans are created"() {
|
||||
expect: "the WebController is created"
|
||||
webController
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.baeldung.boot
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||
import org.springframework.test.web.servlet.MockMvc
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
|
||||
import spock.lang.Narrative
|
||||
import spock.lang.Specification
|
||||
import spock.lang.Title
|
||||
|
||||
@Title("WebController Specification")
|
||||
@Narrative("The Specification of the behaviour of the WebController. It can greet a person, change the name and reset it to 'world'")
|
||||
@AutoConfigureMockMvc
|
||||
@WebMvcTest
|
||||
class WebControllerTest extends Specification {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc
|
||||
|
||||
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(MockMvcResultMatchers.status().isOk()).andReturn().response.contentAsString == "Hello world!"
|
||||
}
|
||||
|
||||
def "when set and delete are performed then the response has status 204 and content changes as expected"() {
|
||||
given: "a new name"
|
||||
def NAME = "Emmy"
|
||||
|
||||
when: "the name is set"
|
||||
mvc.perform(MockMvcRequestBuilders.put("/hello").content(NAME)).andExpect(MockMvcResultMatchers.status().isNoContent())
|
||||
|
||||
then: "the salutation uses the new name"
|
||||
mvc.perform(MockMvcRequestBuilders.get("/hello")).andExpect(MockMvcResultMatchers.status().isOk()).andReturn().response.contentAsString == "Hello $NAME!"
|
||||
|
||||
when: "the name is deleted"
|
||||
mvc.perform(MockMvcRequestBuilders.delete("/hello")).andExpect(MockMvcResultMatchers.status().isNoContent())
|
||||
|
||||
then: "the salutation uses the default name"
|
||||
mvc.perform(MockMvcRequestBuilders.get("/hello")).andExpect(MockMvcResultMatchers.status().isOk()).andReturn().response.contentAsString == "Hello world!"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user