admin管理员组

文章数量:1122846

I have a gradle project with several submodules such as core and adapter. I generate custom assertj asserts for some classes in core. In order to use these asserts in core but also in adapter I use the java-test-fixtures plugin. This works fine with gradle. the projects build and the tests run through. Even with the green play button in intelliJ at the side of test classes and methods the tests work. However IntelliJ itself cannot resolve the classes in the test-fixtures folder.

The static import is marked red with Cannot resolve symbol 'Assertions' The custom method hasFoo is also marked red with Cannot resolve method 'hasFoo' in 'ObjectAssert'.
The problem with this is that I cannot comfortably write tests like this, because I do not get code completion. And I can never be sure if the tests compile unless I run them.

Since everything works in gradle I assume that I have configured everything correctly in gradle and the problem is with IntelliJ which seems to ignore my generated testfixtures folder when highlighting my code.

I would like to understand why IntelliJ cannot resolve the classes when gradle evidently can. And of course how to fix this. Ideally, I do not want to change the gradle config for my IDE (idea plugin for instance), but if there is no other way I'm open to it.

Details

Gradle setup

$ gradle -q projects
Root project 'my-project'
+--- Project ':application'
|    +--- Project ':application:adapter'
|    \--- Project ':application:core'
\--- Project ':bootstrap'

$ cat settings.gradle
rootProject.name = "my-project"
include("bootstrap")
include("application:adapter")
include("application:core")

I haven't explicitly defined a testfixtures subproject, because as far as I can tell, I don't need to.

Relevant parts of the build.gradle of core:

plugins {
    ...
    id("org.assertj.generator") version "1.1.0"
    id("java-test-fixtures")
}

sourceSets {
    main {
        assertJ {
            packages {//package for which to generate asserts
                include("depany.myproject.application.core.domain.tests")
            }

            //where the asserts are written
            outputDir = file("src/testFixtures/java")
        }
    }
}

//ensure asserts are generated before test fixtures are compiled
tasks.named("compileTestFixturesJava") {
    dependsOn(":application:core:generateAssertJ")
}

dependencies {
    testFixturesApi("org.assertj:assertj-core:3.25.1")
}

Relevant parts of the build.gradle of adapter:

dependencies {
    implementation(project(":application:core"))
    testImplementation(testFixtures(project(":application:core")))
}

IntelliJ config

I am using: IntelliJ IDEA 2024.1.5 (Ultimate Edition) on the actual code base where I cannot update to a newer version. I have since managed to reproduce this in a smaller project on a different machine with IntelliJ (up to) 2024.3 (Community Edition)

I have noticed this problem from the start ~5 month ago. => It's not connected to a recent patch. If it used to work with IntelliJ, then before I tried it.

Project tree: The project tree shows the testFixtures as a sibling to main and test in core. its child java is test sources root. I'm not sure what this should look like but it looks like IntelliJ is generally aware of the folder.

Looking at the module settings, we see that testFixtures is recognized as a module.

It also seems to be a dependency of adapter.test:

Minimal Reproducible Example

I managed to compile an MRE:

Solution attempts:

IntelliJ suggestion: add dependency to module

Intellij suggests Add dependency on module 'my-project.application.core.test'. When I click on it something must happen because the suggestion goes away, but the problem does not. After I work through "Repair IDE" the suggestion is back again.

Refresh/Reimport actions

This is not an error that occurs with time, but from the very start. So, deleting the .idea folder and reimporting the project does not work. Neither does "Repair IDE" with includes e.g. invalidate caches and restart.

Using the idea plugin

adding

plugins {
    ...
    idea
}

idea {
    module {
        testSourceDirs += file('src/testFixtures/java')
        testResourceDirs += file('src/testFixtures/resources')
    }
}

to the build.gradle.kts of core does not work due to ambiguity of += and then also because of deprecations. Adding

idea {
    module {
        testSources.from(file("src/testFixtures/java"))
        testResources.from(file("src/testFixtures/resources"))
    }
}

compiles, but IntelliJ still cannot resolve the classes from testFixtures.

Adding

idea {
    module {
        testSources.from(file("../core/src/testFixtures/java"))
        testResources.from(file("../core/src/testFixtures/resources"))
    }
}

to the build.gradle.kts of adapter seems to do the trick, however there is one warning:

Duplicate content roots detected

Path [[...]/debug-microservice/application/core/src/testFixtures/resources] of module [my-project.application.adapter.test] was removed from modules [my-project.application.core.testFixtures]" Also 1 more path was deduplicated. See idea log for details

I have a gradle project with several submodules such as core and adapter. I generate custom assertj asserts for some classes in core. In order to use these asserts in core but also in adapter I use the java-test-fixtures plugin. This works fine with gradle. the projects build and the tests run through. Even with the green play button in intelliJ at the side of test classes and methods the tests work. However IntelliJ itself cannot resolve the classes in the test-fixtures folder.

The static import is marked red with Cannot resolve symbol 'Assertions' The custom method hasFoo is also marked red with Cannot resolve method 'hasFoo' in 'ObjectAssert'.
The problem with this is that I cannot comfortably write tests like this, because I do not get code completion. And I can never be sure if the tests compile unless I run them.

Since everything works in gradle I assume that I have configured everything correctly in gradle and the problem is with IntelliJ which seems to ignore my generated testfixtures folder when highlighting my code.

I would like to understand why IntelliJ cannot resolve the classes when gradle evidently can. And of course how to fix this. Ideally, I do not want to change the gradle config for my IDE (idea plugin for instance), but if there is no other way I'm open to it.

Details

Gradle setup

$ gradle -q projects
Root project 'my-project'
+--- Project ':application'
|    +--- Project ':application:adapter'
|    \--- Project ':application:core'
\--- Project ':bootstrap'

$ cat settings.gradle
rootProject.name = "my-project"
include("bootstrap")
include("application:adapter")
include("application:core")

I haven't explicitly defined a testfixtures subproject, because as far as I can tell, I don't need to.

Relevant parts of the build.gradle of core:

plugins {
    ...
    id("org.assertj.generator") version "1.1.0"
    id("java-test-fixtures")
}

sourceSets {
    main {
        assertJ {
            packages {//package for which to generate asserts
                include("de.company.myproject.application.core.domain.tests")
            }

            //where the asserts are written
            outputDir = file("src/testFixtures/java")
        }
    }
}

//ensure asserts are generated before test fixtures are compiled
tasks.named("compileTestFixturesJava") {
    dependsOn(":application:core:generateAssertJ")
}

dependencies {
    testFixturesApi("org.assertj:assertj-core:3.25.1")
}

Relevant parts of the build.gradle of adapter:

dependencies {
    implementation(project(":application:core"))
    testImplementation(testFixtures(project(":application:core")))
}

IntelliJ config

I am using: IntelliJ IDEA 2024.1.5 (Ultimate Edition) on the actual code base where I cannot update to a newer version. I have since managed to reproduce this in a smaller project on a different machine with IntelliJ (up to) 2024.3 (Community Edition)

I have noticed this problem from the start ~5 month ago. => It's not connected to a recent patch. If it used to work with IntelliJ, then before I tried it.

Project tree: The project tree shows the testFixtures as a sibling to main and test in core. its child java is test sources root. I'm not sure what this should look like but it looks like IntelliJ is generally aware of the folder.

Looking at the module settings, we see that testFixtures is recognized as a module.

It also seems to be a dependency of adapter.test:

Minimal Reproducible Example

I managed to compile an MRE: https://github.com/timo-a/debug-microservice/tree/stacko/79215647

Solution attempts:

IntelliJ suggestion: add dependency to module

Intellij suggests Add dependency on module 'my-project.application.core.test'. When I click on it something must happen because the suggestion goes away, but the problem does not. After I work through "Repair IDE" the suggestion is back again.

Refresh/Reimport actions

This is not an error that occurs with time, but from the very start. So, deleting the .idea folder and reimporting the project does not work. Neither does "Repair IDE" with includes e.g. invalidate caches and restart.

Using the idea plugin

adding

plugins {
    ...
    idea
}

idea {
    module {
        testSourceDirs += file('src/testFixtures/java')
        testResourceDirs += file('src/testFixtures/resources')
    }
}

to the build.gradle.kts of core does not work due to ambiguity of += and then also because of deprecations. Adding

idea {
    module {
        testSources.from(file("src/testFixtures/java"))
        testResources.from(file("src/testFixtures/resources"))
    }
}

compiles, but IntelliJ still cannot resolve the classes from testFixtures.

Adding

idea {
    module {
        testSources.from(file("../core/src/testFixtures/java"))
        testResources.from(file("../core/src/testFixtures/resources"))
    }
}

to the build.gradle.kts of adapter seems to do the trick, however there is one warning:

Duplicate content roots detected

Path [[...]/debug-microservice/application/core/src/testFixtures/resources] of module [my-project.application.adapter.test] was removed from modules [my-project.application.core.testFixtures]" Also 1 more path was deduplicated. See idea log for details

Share Improve this question edited Dec 1, 2024 at 15:57 peer asked Nov 22, 2024 at 15:36 peerpeer 4,65911 gold badges54 silver badges88 bronze badges 3
  • I had this happen with maven, there is this reload all maven projects incrementally and generate sources and update folders for all project buttons. After clicking them most of the times the red go away or after a restart. There might be something similar for gradle, you can also try updating intelllij, 2024.2.4 is latest. – xMilos Commented Nov 25, 2024 at 12:26
  • I've tried version 2024.3 by now, but to no avail. With gradle I have the option to 'Sync all Gradle projects' but that does not help either. I have a feeling that something needs to be configured (instead of refreshed/-generated/-loaded) in IntelliJ as 'cannot be resolved' seems to be default state that is predictably taken after project setup. – peer Commented Nov 25, 2024 at 15:42
  • I would suggest that you add another sourceset to gradle for your test fixtures. Can't really help on the details as my gradle knowledge is a bit encrusted ;) – cyberbrain Commented Nov 26, 2024 at 20:46
Add a comment  | 

1 Answer 1

Reset to default 0

My best answer would be to open a ticket with JetBrains. If you give them your example project, they may be able to fix this problem. I'd also make sure they don't try to merge it into IDEA-235305 (like they did with IDEA-363863), which hasn't even been looked at since 2019. They also said this was fixed in IDEA-302560, but it obviously wasn't.

(I would love to have a REAL fix for you, but I don't have enough reputation to even comment on your question. Otherwise, I'd say, "This is happening to me too!" I really want to add this testFixtures thing to my work projects, but if IntelliJ is going to barf on resolving them, that's dead in the water.)

本文标签: javaIntellij cannot resolve classes from test fixturesStack Overflow