Search code examples
gradlesource-sets

Gradle + Spring Boot - how can I read resources in "itest" sourceSet in a test in "test" sourceSet


I have sourceSet itest, test and main at the save level.

In a test in test sourceSet, I want to read a text file in itest/resources. Is that possible?


Solution

  • Yes, I have found the way.

    Just change sourceSet test configuration: modify resources.srcDirs to add another path to the value set.

    In build.gradle:

    sourceSets {
        test {
            output.resourcesDir = "build/resources/test" // copy test resources to build 
            resources.srcDirs = ['src/test/resources', 'src/itest/resources/fixtures/preAuthMock2']
            // to include example json from itest resources
        }
    }
    

    Note that when you specify this folder which is the direct parent dir of the file you need, when you read the file with getResourceAsStream(), you only need /foo.json as the path, instead of /fixtures/preAuthMock2/foo.json.