Alex Man
Alex Man

Reputation: 4886

How to make resources and java folder test based in gradle

I have created a custom module in sourceSets called as integration for keeping all my integration test cases. The configuration is working fine but the only project is the in the integration folder the resources folder is not Test Resources Root and java folder is not Test Sources Root. I want the intergration folder java and resources to looks exactly like the test module like as shown below.

enter image description here

In the above picture if you observe the java folder looks green under test folder but under integration folder java folder looks blue in color.

What I except: I want the java and resources folder in integration folder looks exactly like that of test folder. ie. If you observe in the screenshot my main and integration folders looks exactly the same. But instead I want the integration folder to make it look exactly like the test folder

My gradle sourceSets look like this

sourceSets {
    integration {
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

In IntelliJ I can change it by right clicking onto the integration>java folder Make Directory as> Test Sources Root

and

integration>resources folder Make Directory as> Test Resources Root

enter image description here

but I would like to know how to achieve this via build.gradle

Can anyone please help me on this

Upvotes: 6

Views: 3750

Answers (2)

Mario Varchmin
Mario Varchmin

Reputation: 3782

Do you want to have this red-green-arrow symbol on the resources folder icon? In Eclipse, you could simply rename your integration sourceset to integrationTest, and the Eclipse gradle plugin would interpret the name accordingly.

In IntelliJ you have to apply the gradle idea plugin and set the testSrcDirs like this:

apply plugin: 'java'
sourceSets {
  integration
}
apply plugin: 'idea'
idea {
  module {
    testSourceDirs += project.sourceSets.integration.java.srcDirs
    testSourceDirs += project.sourceSets.integration.resources.srcDirs
  }
}

Look here: IntelliJ should be aware that a custom sourceSet contains Test sources

Upvotes: 4

gantoin
gantoin

Reputation: 185

Add Integration Test Module

https://ryanharrison.co.uk/2018/07/25/kotlin-add-integration-test-module.html

To add a testIntegration module, you can make some edits to your build.gradle file to define a new source set (IntelliJ module):

sourceSets {
    testIntegration {
        java.srcDir 'src/testIntegration/java'
        kotlin.srcDir 'src/testIntegration/kotlin'
        resources.srcDir 'src/testIntegration/resources'
        compileClasspath += main.output
        runtimeClasspath += main.output
    }
}

You also have to define a new Task to run the integration tests, pointing it to the classes and classpath of the testIntegration source set instead of the inherited defaults from test:

configurations {
    testIntegrationImplementation.extendsFrom testImplementation
    testIntegrationRuntime.extendsFrom testRuntime
}

task testIntegration(type: Test) {
    testClassesDirs = sourceSets.testIntegration.output.classesDirs
    classpath = sourceSets.testIntegration.runtimeClasspath
}

If you run Gradle with the option to ‘Create directories for empty content roots automatically’, you should see a new module get created. You might notice one issue though, the new module is not marked as a test module within IntelliJ. You could do this manually, but it would get reset every time Gradle runs. To override this, you can apply the idea plugin and add the source directories of the new source set:

idea {
    module {
        testSourceDirs += project.sourceSets.testIntegration.java.srcDirs
        testSourceDirs += project.sourceSets.testIntegration.kotlin.srcDirs
        testSourceDirs += project.sourceSets.testIntegration.resources.srcDirs
    }
}

Upvotes: 6

Related Questions