Sometimes, we'll need to test whether a web service has created a file or certain file content, for example, a log message. This recipe looks at a few ways to test file existence and content using Groovy. The examples are fairly simple, but hopefully effective enough for most needs!
Let's start with checking whether a file exists at a given path:
def fileName = "/temp/new_invoices.txt" def testFile = new File(fileName) if (!testFile.exists()) testRunner.fail("File $fileName does not exist.") else log.info "File $fileName exists."
To check whether a file contains a given text string, use the following code:
def fileName = "/temp/catalina.2013-08-23.log" def searchString = "o12345" def testFile = new File(fileName) def found = false testFile.eachLine{line -> if (line.contains(searchString)) { log.info "Found in line: $line" found = true } } if (!found) ...