cannot find symbol -- integration test class depends on unit test class

Answered

We have a Java project with three source sets: main, test, integTest. There is a compile-time dependency between integTest and test (integTest depends on a class from test).

The project builds and runs from the command line (Gradle 4.6). Other developers on my team are able to "Build > Rebuild Project" from within IntelliJ (latest version and recent versions). When I do this IntelliJ complains that it "cannot find symbol", where the symbol it can't find is the class in the test sources.

I have reproduced this after creating a new (Ubuntu) virtual machine with a fresh install of IntelliJ and a simplified version of the project.

Worst part (IMHO): this used to work for me and I'm not sure when it broke as I rarely rebuild the project.

We have two other projects with similar structure/dependencies.

Any idea what could be happening here? (I will try to attach/include the build script separately.)

0
5 comments

Here's the build script:

 

```

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'

buildscript {
    repositories {
        maven { url "http://repo.maven.apache.org/maven2/" }
    }
    ext {
        springBootVersion = '1.5.9.RELEASE'
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.1'
    }
}

repositories {
    maven {
        url "http://repo.maven.apache.org/maven2/"
    }
}

archivesBaseName = 'location-service'
group = 'org.byteworks.app'
version = '0.0.2-SNAPSHOT'
sourceCompatibility = 1.8

loadEnvironmentConfiguration()

def loadEnvironmentConfiguration() {
    String environment = hasProperty('env') ? property('env') : 'dev'

    def configFile = file('config.groovy')
    ext.config = new ConfigSlurper(environment).parse(configFile.toURI().toURL())
}

sourceSets {
    integTest {
        java {
            compileClasspath += test.output
            runtimeClasspath += test.output
            srcDir file('src/integTest/java')
        }
    }
}

configurations {
    integTestCompile.extendsFrom testCompile
    integTestRuntime.extendsFrom testRuntime
}

configurations.all {
    exclude group: "commons-logging", module: "commons-logging"
}

dependencies {
    compile 'org.apache.commons:commons-text:1.2'
    compile 'org.apache.commons:commons-lang3:3.7'
    compile 'org.slf4j:jcl-over-slf4j:1.7.25'
    compile 'ch.qos.logback:logback-classic:1.2.3'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.cloud:spring-cloud-contract-wiremock:1.2.2.RELEASE'
    testCompile project(':').sourceSets.main.output
    testCompile 'com.github.tomakehurst:wiremock:2.1.12'
    testCompile 'io.rest-assured:rest-assured:3.0.6'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.2'
    testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.0.2'
}

bootRun {
    systemProperties = System.properties as Map<String, ?>
}

test {
    useJUnitPlatform()
}

task integTest(type: Test) {
    check.dependsOn(it)
    group = JavaBasePlugin.VERIFICATION_GROUP
    testClassesDirs = sourceSets.integTest.output.classesDirs
    classpath = sourceSets.integTest.runtimeClasspath
    outputs.upToDateWhen { false }
}

task printVersion {
    println project.version
}

```

 

0
Avatar
Yaroslav Bedrov

Hello Thomas,

Do you have the same state for "Create separate module per source set" option on your machine and on other machines?

0

Hi Yaroslav,

I've been using the default (enabled). I just tried importing with this setting turned off and now the integTest sources fail to compile because they can't find org.junit.Test.

 

0

Update: a co-worker just deleted the project, re-imported it, and now has the same trouble as me. So at least it's reproducible across multiple people/environments now....

Maybe something wrong with our build.gradle? Can I upload a tgz of our project somewhere for someone to try?

0

Update after spending some time on this with a co-worker whose gradle-fu is stronger than mine, the problem is fixed. There are two solutions:

(1) Add the following lines to the gradle script:

 

idea {
    module {
        sourceSets.integTest.allSource.srcDirs.each { srcDir -> module.testSourceDirs += srcDir }
    }
}

 

(2) Use the Netflix nebula project plugin:

buildscript { dependencies {
    classpath 'com.netflix.nebula:nebula-project-plugin:4.0.1'
} }

apply plugin: 'nebula.integtest'

 

 

0

Please sign in to leave a comment.