2

I would like to add integration tests to my spring boot application. Currently I don't have unit tests but I want to keep the standard project layout:

├── Demo
│   ├── src
│   │   ├── main
|   |   |   └──java
│   │   ├── integrationTest
|   |   |   └──java
│   │   ├── test
|   |   |   └──java

My questions are:

Should I create integrationTest folder as module? If so - why? and how can I do it in intellij?

'main' and 'test' are already defined as modules under 'Demo' which is also defined as module.

I've tried several times to set it via 'Project Structure' but it wasn't created under 'Demo' and caused to a mess by creating a different src folder and another build.gardle file

and when I succeeded to create it under 'Demo', it added a new line to setting.gradle file: include 'integrationTest'

I'm really confused with it.

Can anyone help me how to create this submodule (if needed)?

Thanks

R TZ
  • 61
  • 8
  • That exact project structure is documented in the Gradle user guide [here](https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests). – Bjørn Vester Apr 05 '21 at 20:05
  • @BjørnVester thanks! I've actually just found the solution you suggested in another forum and it's very close to what I need but it creates the module as 'Source' instead of 'Test Source' and it doesn't help me. This seems to be a [known issue](https://stackoverflow.com/questions/36324424/defining-a-test-source-set-for-intellij-15-with-gradle). My manager don't want me to spend a lot of time on it and told me to create both 'integrationTest' and 'unitTest' under 'test' module in different packages. I don't like it but this seems to be something that works. – R TZ Apr 06 '21 at 03:33

2 Answers2

3

I'll put here the solution for people who encounter the same problem.

Creating a module for integrationTest (add the following to build.gradle):

sourceSets {
    integrationTest {
        java.srcDir file("src/integrationTest/java")
        resources.srcDir file("src/integrationTest/resources")
        runtimeClasspath += sourceSets.main.runtimeClasspath + sourceSets.test.runtimeClasspath
        compileClasspath += sourceSets.main.compileClasspath + sourceSets.test.compileClasspath
    }
}

Mark it as 'Test Source' in Intellij:

plugins {
    // ...
    id 'idea'
}
// ...
idea {
    module {
        sourceSets.integrationTest.allSource.srcDirs.each { srcDir -> module.testSourceDirs += srcDir }
    }
}
// ...
wheleph
  • 7,974
  • 7
  • 40
  • 57
R TZ
  • 61
  • 8
0

Extending the accepted answer, this is how you do it in Kotlin DSL:

sourceSets {
    create("integrationTest") {
        java.srcDir("src/integrationTest/scala")
        resources.srcDir("src/integrationTest/resources")
        runtimeClasspath += sourceSets["main"].runtimeClasspath + sourceSets["test"].runtimeClasspath
        compileClasspath += sourceSets["main"].compileClasspath + sourceSets["test"].compileClasspath
    }
}

idea {
    module {
        val integrationTestSourceSet = sourceSets["integrationTest"]
        integrationTestSourceSet.allSource.srcDirs.forEach { srcDir -> testSourceDirs.add(srcDir) }
    }
}
Praytic
  • 1,771
  • 4
  • 21
  • 41