Skip to content

Instantly share code, notes, and snippets.

@frangarcia
Created May 24, 2020 11:05
Show Gist options
  • Save frangarcia/4ffdab304c6b92db38f06b1e81c015de to your computer and use it in GitHub Desktop.
Save frangarcia/4ffdab304c6b92db38f06b1e81c015de to your computer and use it in GitHub Desktop.
@Grapes([
@Grab(group='org.spockframework', module='spock-core', version='1.3-groovy-2.5', scope='test'),
@Grab(group='org.testcontainers', module='spock', version='1.14.2', scope='test'),
])
import spock.lang.*
import org.testcontainers.containers.GenericContainer
import groovy.json.JsonSlurper
@org.testcontainers.spock.Testcontainers
class UserControllerSpec extends Specification {
@Shared
GenericContainer prism = new GenericContainer<>("stoplight/prism:3.3.4")
.withFileSystemBind(new File("").getAbsolutePath(),"/tmp")
.withCommand("mock -h 0.0.0.0 -d /tmp/swagger.yml")
.withExposedPorts(4010)
@Shared
UserController userController
def setupSpec(){
prism.start()
userController = new UserController("http","localhost",prism.firstMappedPort)
}
def cleanupSpec() {
prism.stop()
}
def "Getting the list of user"(){
when:
List<String> userList = userController.getUsers()
then:
userList.size() > 0
and:
userList.each { String user ->
assert user instanceof String
}
}
}
class UserController {
String protocol
String host
Integer port
UserController(String protocol, String host, Integer port) {
this.protocol = protocol
this.host = host
this.port = port
}
List<String> getUsers() {
List<String> userList = []
String usersJson = new URL("${protocol}://${host}:${port}/users").text
JsonSlurper jsonSlurper = new JsonSlurper()
def json = jsonSlurper.parseText(usersJson)
json.each {
userList.add(it)
}
return userList
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment