org.gradle.testfixtures.ProjectBuilder, org.gradle.api.Project & logging


I’m a n00bie to Groovy and I am writing some unit tests using the spock framework, and am using the


to create a Project object.


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

The code that I am testing uses the Project object’s logger to log information. I would like to be able to set a logger in the Project object so that in my unit test I can also capture logging from the method itself to help debug test failures for example.

e.g. capture the following debug statement XYZ when I test that method:

public Object myMethodThatIAMTesting(Project project, ...)

Doing project.setProperty(‘logger’, Object) says I can’t set a read only value… and I can’t figure out what Object to put to satisfy doing project.getLogging().addStandardOutputListener( ??? )

So how do I do this? I can’t seem to figure it out :frowning:




Take a look project.getLogging():

If that doesn’t help let us know, there should be a way of doing that using slf4j api I think.

No cigar :frowning: If the source code is still like that then it creates a NoOpLoggingManager which has empty methods… and when I try to stick other objects in, the stack trace does indeed say that a NoOpLoggingManager is being used :frowning:

and I see no API to enable me to set the logger in the ProjectBuilder class.

The simpler solution is to explicitly pass in a logger I guess… oh well.

The simpler solution is to explicitly pass in a logger I guess… oh well.

There you go :slight_smile: A bit of refactoring and things are getting easier to test.

Sorry, the Project that is built by the ProjectBuilder is not a full blown gradle project but a useful mock for tests.

actually creating a new slf4j logger doesnt do anything since I have dont have access in the test to set the log level? or do I? :confused:

I’m a bit lost as to where I would configure the logger that I explicitly create… :confused:

Since we’re talking about unit testing, perhaps there is a way to refactor the code a little bit to make it easier to test? Wrap the call in something verifiable or use a mocking framework?

yeah Spock doesnt seem to have anything about logging… :confused: oh well.