IntelliJ tests not compiling due to test fixtures

Hi,

In IntelliJ my tests are showing errors due to an inability to see source code from my test fixtures. But everything seems to build correctly (using IntelliJ and Gradle) and my tests actually execute successfully…

So in IntelliJ I’m seeing red compilation errors:

The AbstractTest class is a “test fixture”. You can see it inside the “testFixtures” folder inside the directory structure on the left-hand side.

Maybe its an IntelliJ issue? Because using Gradle directly seems to work and I can even execute the test from within IntelliJ successfully…

I have two modules: “:shared:utils” and “:shared:aws-api”, and both modules have the same issue with test fixtures.

My root build.gradle:

plugins {
id “base”
id “idea”
// Gradle - Plugin: org.owasp.dependencycheck
// Using Gradle Plugins
// dependency-check – Usage
id “org.owasp.dependencycheck” version “6.5.1” apply false
}

defaultTasks = [“build”]

subprojects {
// Gradle - Plugin: org.owasp.dependencycheck
// Using Gradle Plugins
apply plugin: “org.owasp.dependencycheck”

// IDE configuration.
// Download the source code of jar dependencies.
// https://stackoverflow.com/questions/10156847/how-to-tell-gradle-to-download-all-the-source-jars
idea {
    module {
        downloadSources = true
    }
}

}

My settings.gradle:

rootProject.name = “automated_trading”
include “:shared:utils”
include “:shared:aws-api”

The build.gradle of my “:shared:utils” module

plugins {
id “idea”
id “java-library”
// Testing in Java & JVM projects
id “java-test-fixtures”

// Plugin for better test output
// https://stackoverflow.com/questions/3963708/gradle-how-to-display-test-results-in-the-console-in-real-time
// https://github.com/radarsh/gradle-test-logger-plugin
id "com.adarshr.test-logger" version "3.1.0"

}

version = “1.0.0”
defaultTasks = [“build”]
sourceCompatibility = 1.16 // I tried 1.17, but something isn’t right with Gradle 7.3.3 - even though they say they support 1.17

// ╔═══════════════════════════════════════════════════════════════════════════╗
// ║ Repositories ║
// ╚═══════════════════════════════════════════════════════════════════════════╝
repositories {
mavenCentral();

maven {
    // for snakeyaml
    // https://central.sonatype.org/pages/ossrh-guide.html
    url "https://oss.sonatype.org/content/groups/public/";
}

}

// ╔═══════════════════════════════════════════════════════════════════════════╗
// ║ Dependencies ║
// ╚═══════════════════════════════════════════════════════════════════════════╝
dependencies {
// ╔═════════════════════╗
// ║ Module dependencies ║
// ╚═════════════════════╝

// ╔════════════════════════════╗
// ║ Miscellaneous dependencies ║
// ╚════════════════════════════╝
// https://mvnrepository.com/artifact/org.apache.commons/commons-text
implementation "org.apache.commons:commons-text:1.9";

// https://mvnrepository.com/artifact/org.yaml/snakeyaml
// https://bitbucket.org/asomov/snakeyaml/src/default/
implementation "org.yaml:snakeyaml:1.28";


// ╔══════════════════════╗
// ║ Logging dependencies ║
// ╚══════════════════════╝
// SLF4J
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation "org.slf4j:slf4j-api:1.7.32"

// Log4j2
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-bom
// Not as per the Log4j2 documentation (couldn't get their way to work and it seems more recondite)
// https://logging.apache.org/log4j/2.x/maven-artifacts.html
implementation platform("org.apache.logging.log4j:log4j-bom:2.17.1")
implementation "org.apache.logging.log4j:log4j-api"
implementation "org.apache.logging.log4j:log4j-core"

// SLF4J bridge for Log4j2
// Existing components that use SLF4J will have their logging routed to Log4j2
implementation "org.apache.logging.log4j:log4j-slf4j-impl"


// ╔═══════════════════╗
// ║ Test dependencies ║
// ╚═══════════════════╝
// https://www.baeldung.com/junit-5-gradle
testImplementation "org.junit.jupiter:junit-jupiter-api:5.8.2"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.8.2"


// ╔═══════════════════════════╗
// ║ Test fixture dependencies ║
// ╚═══════════════════════════╝
testFixturesImplementation "org.slf4j:slf4j-api:1.7.32"

}

// ╔═══════════════════════════════════════════════════════════════════════════╗
// ║ Other configuration ║
// ╚═══════════════════════════════════════════════════════════════════════════╝
// Generate a sources JAR for the library
// Building Java Libraries Sample
java {
withSourcesJar()
}

// Use JUnit 5 for tests
// Testing in Java & JVM projects
test {
useJUnitPlatform()
}

// IDE configuration.
// Download the source code of jar dependencies.
// how to tell gradle to download all the source jars - Stack Overflow
idea {
module {
downloadSources = true
}
}

// Add some entries to the manifest file
// Building Java Libraries Sample
tasks.named(“jar”) {
manifest {
attributes(“Implementation-Title”: project.name,
“Implementation-Version”: project.version)
}
}

Any help or insight appreciated. I’ve tried modifying the “test” sourceSet to include the files from the test fixtures - but without success. I’m not a Gradle guru, so maybe I just approached it incorrectly…

Don’t think its relevant, but the “:shared:utils” and “:shared:aws-api” modules are Git submodules.

Thank you for your time and attention

Okay, seems there’s actually a compilation issue inside the test fixtures folder as well… It can’t see something inside the same test fixtures folder, but inside a different package:

Should I perhaps do away with the “java-test-fixtures” plugin and try and get something similar to work using sourceSets?

I’m using the latest Gradle v7.3.3

Build files attached
buildFiles.zip (2.1 KB)

I removed the “java-test-fixtures” plugin for now and just created my own sourceSet:

sourceSets {
    test {
        java {
            compileClasspath += sourceSets.main.output
            runtimeClasspath += sourceSets.main.output
            srcDirs "src/testFixtures/java"
        }
        resources {
            srcDirs "src/testFixtures/resources"
        }
    }    
}

I’m going to do some more testing later, but everything seems to be working for now, even though I had to do away with the “java-test-fixtures” plugin.

The test fixtures plugin basically also just configures some source sets.
I guess more that IntelliJ had some corrupt cache.
I’d try it here, but your zip only contains the build files, not an MCVE.
Put back in the test fixtures plugin and refresh the Gradle project.
If it still does not work, try “Invalidate Caches and Restart”.

Thank you Björn. I tried IntelliJ’s “Invalidate Caches and Restart” feature - but unfortunately, no success.

Kindly find an MCVE attached.
automated_trading_mvce.zip (782.4 KB)

If I build the MCVE from the command line using Gradle everything seems to work:

From within IntelliJ everything doesn’t seem to be working however:

So my test can’t see “test fixtures” :frowning:

Any help or insight appreciated, thank you - appreciated

Let me know if you think I should rather raise my question with the IntelliJ team :+1: :slightly_smiling_face:

Works perfectly fine here, with and without delegation to Gradle.
So it must be something with your local setup I’d say.

Not sure what the issue can be, but thanks for checking Björn.

I’ll stick with the use of the SourceSet for now.

:+1: :slightly_smiling_face:

If you just want to work-around, I’d recommend re-enabling the delegation to Gradle.
It is most often the better choice as Gradle should know exactly how to build the files and run the tests.
So even if you have specialities like you need some environment variables or system properties or whatever else, it will just work.
And it should also work in your case, as the editor is not showing red, just the compiler is misbehaving.

And yes, I guess you should report this also to JetBrains.