Adding outputListeners in a junit Test

(detlef.brendle) #1


I want to add a standardOutputListener to a task/project within a junit testcase. I do:


void testListenToSdtOut() {

def project = ProjectBuilder.builder().build()

project.tasks.add(name: ‘hello’) << {

println “running task”


Task task = project.tasks[‘hello’]

assert task

boolean outputCaptured

task.logging.addStandardOutputListener([onOutput: {line ->

println ‘output captured.’

outputCaptured = true

}] as StandardOutputListener)


assert outputCaptured


But my testcase stays red. I investigated within the sources that there is a NoOpLoggingManager that does nothing when I call ‘add*Listener()’ Is there a specific reason why this is done like this ? I think it would be nice if the Testcase could also listen to standard output messages from a task.

What do you think ?

thanks, detlef

PS - Im using milestone-8

(Peter Niederwieser) #2

‘ProjectBuilder’ is meant for lower-level tests that don’t execute anything. More comprehensive testing support for Gradle plugins is in the pipeline. Meanwhile, you can (with some effort) kick off a real build from your test (via GradleLauncher).

(detlef.brendle) #3

Hi Peter, thanks for pointing me to the right direction. GradleLauncher is fine for me.

Regards, detlef

(Luke Daley) #4

You can find one example of how to do this here: