Gradle equivalent of Surefire classpathDependencyExclude
Asked Answered
D

2

8

I'm trying to migrate java project from maven to gradle. The problem is very tricky classpath dependency configuration for tests now.

Our maven-surefire-plugin configuration:

 <includes>
     <include>**/SomeTest1.java</include>
 </includes>
 <classpathDependencyExcludes>
     <classpathDependencyExclude>com.sun.jersey:jersey-core</classpathDependencyExclude>
 </classpathDependencyExcludes>

There are different classpathes for different test-classes. How can I implement it with Gradle?

Disarrange answered 25/8, 2015 at 15:47 Comment(0)
M
8

First of all you need to differ your tests sources into separate sourceSets. Say, we need to run tests from package org.foo.test.rest with a little different runtime classpath, than other tests. Thus, its execution will go to otherTest, where remain tests are in test:

sourceSets {
    otherTest {
        java {
            srcDir 'src/test/java'
            include 'org/foo/test/rest/**'
        }
        resources {
            srcDir 'src/test/java'
        }
    }
    test {
        java {
            srcDir 'src/test/java'
            exclude 'org/foo/rest/test/**'
        }
        resources {
            srcDir 'src/test/java'
        }
    }
}

After that, you should make sure that otherTest has all required compile and runtime classpaths set correctly:

otherTestCompile sourceSets.main.output
otherTestCompile configurations.testCompile
otherTestCompile sourceSets.test.output
otherTestRuntime configurations.testRuntime + configurations.testCompile

The last thing is to exclude (or include) unneeded runtime bundles from test:

configurations {
    testRuntime {
        exclude group: 'org.conflicting.library'
    }
}

And to create Test Gradle task for otherTest:

task otherTest(type: Test) {
    testClassesDir = sourceSets.otherTest.output.classesDir
    classpath += sourceSets.otherTest.runtimeClasspath
}

check.dependsOn otherTest
Mestas answered 21/9, 2015 at 8:30 Comment(1)
I did so and got > Could not find method otherTestCompile() for arguments [main classes] on project. Any ideas what could be wrong?Overjoy
M
2

Use next workaround:

  1. Create source set for needed tests
  2. Add configurations for created sourceSet
  3. Add task for run test with custom configuration
  4. Configure test task dependOn customized test task
  5. Configure Report plugin for generate beautiful html report :)

Like this getting started

Musaceous answered 26/8, 2015 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.