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
}
}
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 wayprintln "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 butwhere:
. Yourlogger.info "hey"
should not even compile and the error you see should be: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.