# Separate execution for Java Unit and Integration tests

**URL:** <https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713>\
**Category:** Help/Discuss\
**Created:** [April 8, 2015, 4:11am UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713 "2015-04-08T04:11:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![avaz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/avaz/32/8440_2.png) [@avaz](https://discuss.gradle.org/u/avaz)\
**Post date:** [April 8, 2015, 4:11am UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713/1 "2015-04-08T04:11:18Z")

</div>

Hi,

I have the following configuration in my gradle build related with unit and integration tests (I’m using JavaPlugin):

```groovy
sourceSets {
  test {
    java.srcDir 'src/test/unit/java'
  }
  integration {
    java.srcDir 'src/test/integration/java'
    resources.srcDir 'src/test/resources'
    compileClasspath += main.output + test.output
    runtimeClasspath += main.output + test.output
  }
}

configurations {
  integrationCompile.extendsFrom testCompile
  integrationRuntime.extendsFrom testRuntime
}

task integration(type: Test, description: 'Runs the integration tests.', group: 'Verification') {
  testClassesDir = sourceSets.integration.output.classesDir
  classpath = sourceSets.integration.runtimeClasspath
}

```

When I run `gradle integration`, only the test classes in the `integration``java.srcDir` SourceSet are executed, but when I run `gradle test` all tests classes are executed, the ones from `test` and `integration` SourceSet’s.  
There is a way to run this tests separated, so `gradle test` execute tests only from `test` SourceSet and `gradle integration` execute tests from `integration` SourceSet?

Thanks!

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [April 8, 2015, 4:37am UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713/2 "2015-04-08T04:37:41Z")

</div>

Are you using an IDE like IntelliJ by any chance? It’s possible that your IDE is compiling both integration and unit tests to the same output directory which is causing Gradle to run all your tests when executing the `test` task.

---

<div class="post-metadata">

**Author:** ![avaz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/avaz/32/8440_2.png) [@avaz](https://discuss.gradle.org/u/avaz)\
**Post date:** [April 8, 2015, 4:43am UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713/3 "2015-04-08T04:43:04Z")

</div>

@mark_vieira Yes, I’m using IntelliJ and you are correct, I executed `gradle clean` followed by a `gradle test` in Terminal and only classes in the `test` was executed.

Do you know how I can avoid this behavior in this IDE?

Thanks!

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [April 8, 2015, 4:49am UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713/4 "2015-04-08T04:49:28Z")

</div>

The IDE is trying to be smart by syncing up its output directory with the one Gradle is using. However, it’s not smart enough to support more than two source sets (main and test). You’ll want to change the output directory in IntelliJ to somewhere other than where Gradle is compiling two, that way they won’t clash. If you use the ‘idea’ plugin you can run the ‘idea’ task to generate IntelliJ metadata rather than using the built-in Gradle import. By default this will use the regular ‘out’ directory for compiled output.

---

<div class="post-metadata">

**Author:** ![avaz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/avaz/32/8440_2.png) [@avaz](https://discuss.gradle.org/u/avaz)\
**Post date:** [April 8, 2015, 5:29am UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713/5 "2015-04-08T05:29:35Z")

</div>

@mark_vieira Just make sure that I understood you correctly, what I need to do is:  
say that my gradle is building my project in ‘PROJECT\_ROOT/build’ and so IDEA. I will need to change the IDEA to build in somewhere else, say ‘PROJECT\_ROOT/build-idea’? Is that correct?

And taking the opportunity, if I’ve understand you correctly, do know if this change have any impact on some behavior or feature of the IDEA itself? Something like code assist or anything like?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Rene](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/rene/32/8082_2.png) [@Rene](https://discuss.gradle.org/u/Rene)\
**Post date:** [April 8, 2015, 6:51am UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713/6 "2015-04-08T06:51:56Z")

</div>

one remark about your build script:

```
test {
    java.srcDir 'src/test/unit/java'
}

```

adds a source directory to the default directory (`src/test/java`). instead you should use

```
test {
    java.srcDirs = ['src/test/unit/java']
}

```

same problem occurs in your integration source set configuration.

Does configuring the srcDirs fix the issue for you?

---

<div class="post-metadata">

**Author:** ![avaz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/avaz/32/8440_2.png) [@avaz](https://discuss.gradle.org/u/avaz)\
**Post date:** [April 8, 2015, 1:07pm UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713/7 "2015-04-08T13:07:30Z")

</div>

@Rene I’ve changed my configuration as you suggested and the problem with IntelliJ IDEA persist. As soon I `make` (`CRTL/COMMAND+F9`) the project in IDEA the classes from integration tests are copied to `build/classes/test`, the same target of `unit tests`, and after that if a I run `gradle test` all tests are executed.  
To improve my knowledge on gradle, what is the impact of your suggestion, this is the best/recommended way to declare `SourceSets`?

And BTW, the suggestion of @mark_vieira do solve the problem, as long the IDEA build the classes in other place than gradle they don’t clash and everything works well.

Thanks for help guys.

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [April 8, 2015, 4:10pm UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713/8 "2015-04-08T16:10:38Z")

</div>

> [@avaz](#):
>
> To improve my knowledge on gradle, what is the impact of your suggestion, this is the best/recommended way to declare SourceSets?

By default the Java plugin looks for test src in ‘src/test/java’. Using your original method you are adding an _additional_ source directory. So Gradle will now look in both directories. If that other directory doesn’t exist, then you really won’t see any difference.

---

<div class="post-metadata">

**Author:** ![kovacserik](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kovacserik](https://discuss.gradle.org/u/kovacserik)\
**Post date:** [October 10, 2017, 11:14am UTC](https://discuss.gradle.org/t/separate-execution-for-java-unit-and-integration-tests/8713/9 "2017-10-10T11:14:39Z")

</div>

Hi, For me this was the solution:

in build.gradle

task unitTest(type: Test, description: ‘Runs the unit tests.’, group: ‘Verification’) {  
useJUnit{  
includeCategories ‘com.my.package.MyCategory’  
}  
}

in test code:

@Category({MyCategory.class})  
public class MyTests {  
}
