Reputation: 31610
While writing tests I sometimes just want to write text to the console for debugging.
How do I do this in a spock test? I tried using the logger but it's throwing an error:
import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.plugin.JenkinsJobManagement
import org.junit.ClassRule
import org.jvnet.hudson.test.JenkinsRule
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
import java.util.logging.Logger
class JobScriptsSpec extends Specification {
@Shared
@ClassRule
JenkinsRule jenkinsRule = new JenkinsRule()
Logger logger = Logger.getLogger("")
@Unroll
def 'test script #file.name'(File file) {
given:
def jobManagement = new JenkinsJobManagement(System.out, [:], new File('.'))
when:
new DslScriptLoader(jobManagement).runScript(file.text)
then:
noExceptionThrown()
where:
// This throws an error
logger.info ("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz")
file << jobFiles
}
static List<File> getJobFiles() {
List<File> files = []
new File('jobs').eachFileRecurse {
if (it.name.endsWith('.groovy')) {
files << it
}
}
files
}
}
Upvotes: 1
Views: 1553
Reputation: 67407
Groovy behaves no different from Java in this regard. For the most part Groovy is a superset of Java syntax. So you can write System.out.println("hey")
or do it the groovy way println "hey"
.
Using loggers is also fine. Maybe you should not log anything in the where:
block because there Spock expects your data tables or data providers, not procedural code. Put the code into the block where it belongs, i.e. anywhere else but where:
. Your logger.info "hey"
should not even compile and the error you see should be:
Error:(18, 5) Groovyc:
where-blocks may only contain parameterizations
(e.g. 'salary << [1000, 5000, 9000]; salaryk = salary / 1000')
P.S.: Having seen your last two questions, I really recommend to first read a Groovy tutorial and then the Spock manual before asking more questions. No offence meant, I just want to help you help yourself. 🙂
Upvotes: 2