Mahoni
Mahoni

Reputation: 7466

How to guarantee that TestNG in gradle adds the JDK to IntelliJ's classpath?

I am using the gradle plugin idea and added TestNG to the dependencies as:

testCompile 'org.testng:testng:6.9.6'

while defining TestNG in the test block

test {
    useTestNG()
}

When I synchronize Gradle in IntellJ now and try to add a test I get:

TestNG could not be found in the project module

Effectively I need to add it manually to the classpath of IntellJ and what is more, even after adding it, the dependencies in my test files cannot be resolved:

error: package org.testng does not exist

import static org.testng.Assert.*;

How should I approach this to fully automatize it?


It seems to work when I change testCompile into compile. I tried the same for JUnit. Same effect. How can that be? testCompile extends compile. Has this to do with where my test files are? How can I change their location with sourceSets?

Upvotes: 1

Views: 893

Answers (1)

darkbit
darkbit

Reputation: 143

Gradle is a bit like Maven: It expects to find your tests in src/test/java. This helps to split your tests from your actual source code, so that the tests won't get into the final jar.

testCompile does indeed extend compile, but that only mean, that every dependency from compile is also a dependency for testCompile, but not the other way!

To change your source sets, you can do something like this:

sourceSets {
    test {
        java { srcDir 'src/main/tests' }        
    }
}

If you want all code to be in the src/main/java folder, you need to exclude the specific packages:

sourceSets {
    main {
        java { exclude 'tests/** ' }
    }
    test {
        java { 
            srcDir 'src/main/java'
            include 'tests/**'
        }        
    }
}

Here the tests package is only used for the tests, while the other packages are in the main program.

See also: https://docs.gradle.org/current/userguide/java_plugin.html#N12323

Upvotes: 2

Related Questions