Skip to content

Instantly share code, notes, and snippets.

@michmzr
Last active October 27, 2022 14:29
Show Gist options
  • Save michmzr/bec858eb65038269a4c4cff43d5ab051 to your computer and use it in GitHub Desktop.
Save michmzr/bec858eb65038269a4c4cff43d5ab051 to your computer and use it in GitHub Desktop.
Spock test utils
import org.json.JSONObject
import spock.lang.Specification
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
class BaseTestSpec extends Specification {
String loadFile(String classPath) throws IOException {
Path resourceDirectory = Paths.get("src", "test", "resources");
String absolutePath = resourceDirectory.toFile().getAbsolutePath()
Files.readString(Paths.get(absolutePath, classPath), StandardCharsets.UTF_8)
}
String loadJsonFile(String classPath) {
parseJSONFile(classPath).toString()
}
JSONObject parseJSONFile(String classPath) throws IOException {
parseJsonString(loadFile(classPath))
}
JSONObject parseJsonString(String string) {
new JSONObject(string)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment