Testing project setup of buildSrc

gradle-core

(Thibault Kruse) #1

Hi,

I am experimenting with moving most reusable buildLogic into the buildSrc folder, like this:

// build.gradle
SimpleProjectSetup.configure(project)

// buildSrc/src/groovy/SimpleProjectSetup.groovy
static void configure(Project project) {
    project.with() {
        apply plugin: 'java'
        check.mustRunAfter(clean)
    }
}

The goal is to keep the build.gradle files of my root folder and submodules as empty as possible, declaring only the “What” of builds, not the “How”.

(I understand that this way I cannot use all syntax that is valid in .gradle files, but I consider that a beneficial constraint. I also know this can be achieved using gradle files and apply from, but I think that makes the project setup much more difficult to understand, and IDEs do not follow such links.)

Now doing so I also wish I could write JUnit test for the above groovy files. For that however, I need a Project instance that I can pass into the class. I tried the gradle testKit, but temporary projects would not have access to the classes in buildSrc. Also I tried mocking Project.class with Mockito, but as an example the syntax project.check.mustRunAfter(project.clean) will not work with Mockito mocks, I get PropertyMissingExceptions

Is there any good trick to achieve this? Or maybe just a clean minimal way to refer to tasks (and without creating them as in project.task(‘check’)).


(Schalk Cronjé) #2

Silly question first, but have you tried using tye ProjectBuilder builder?


(Thibault Kruse) #3

I had not, thanks. That might be the solution. I did not see it mentioned in the documentation I found when searching online for testing gradle scripts.