I have a project with the following directory structure:
src/main/java
src/main/resources
src/test/java
src/test/resources
I want to add a new folder, integrationTest
:
src/integrationTest/java
src/integrationTest/resources
Where I want to keep integration tests totally separate from unit tests. How should I go about adding this? In the build.gradle, I'm not sure how to specify a new task that'd pick this folder build it and run the tests separately.
Unfortunately Maven only supports one test source folder and one test output folder. With mavens build-helper plugin I could add another test-source folder but the compiled classes will be generated into test-classes but I want to compile the classes from src/integration/java into target/integration-test-classes. Is this possible?
The test source set contains the outputs of main in its compile classpath and also includes its dependencies. Next, let's create our unit test: Here we test a simple POJO that stores two values. We can use it directly because the main outputs are in our test classpath. Next, let's run this from Gradle:
If you only want to change the unit test source folder (rather than add an additional one), just change the testSourceDirectory element: This is useful if all of your unit tests are written in groovy. (But you will also need to configure maven to compile your groovy code too - see the groovy-eclipse-maven-plugin or the build-helper-maven-plugin .)
Another way we could configure a separate DataSource for testing is by leveraging Spring Profiles to define a DataSource bean that is only available in a test profile. For this, we can use a .properties file as before, or we can write the values in the class itself.
Gradle has a concept of source sets
which is exactly what you need here. You have a detailed documentation about that in the Java Plugin documenation here : https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets
You can define a new source set "integrationTest" in your build.gradle
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration-test/java')
}
resources.srcDir file('src/integration-test/resources')
}
}
This will automatically create new configurations integrationTestCompile
and integrationTestRuntime
, that you can use to define an new Task integrationTests
:
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
For reference : a working full example can be found here : https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With