IntelliJ can't run unit tests in hierarchical gradle project.

Answered

 

I have a modular project that looks like this:

master-project (top-level)

- common-core (module)

When I try to run unit tests from inside common-core, I get a strange exception that says "Project 'common-core' not found in root project 'common-core'".  I don't understand what this is saying or why.  The project structure is specified by Gradle, where in the setup is like this:

master-project/settings.gradle:

rootProject.name = 'master-project'

include 'common-core'

common-core/settings.gradle: 

rootProject.name = 'common-core'

This builds correctly with gradle build, so I don't understand why running JUnit tests from within the IDE are having this problem.  I have tried this both by right mouse clicking inside a test method and select "Run LoggerTest.usage_example()".  The full Error from the output console is pasted below:

Testing started at 10:38 AM ...
10:38:28 AM: Executing external tasks ':common-core:cleanTest :common-core:test --tests "myModule.LoggerTest.usage_examples"'...
FAILURE: Build failed with an exception.
* What went wrong:
Project 'common-core' not found in root project 'common-core'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 3.38 secs
Project 'common-core' not found in root project 'common-core'.

This doesn't make sense.  I tried to do some basic research to figure this out, but cannot find anything about this problem specifically.  I'm probably doing something wrong here, because this is a basic use case with a hierarchal Gradle project in IntelliJ.  I am using v.2016.1 Ultimate.

Stuck right now and need a nudge in the right direction.

Thanks,

Joel

3 comments

You have common-core/settings.gradle file. It means that common-core is the root of some gradle project. under  common-core folder.

And for the project import in IntelliJ you use another folder: master-project, am I right?

And hence, IntelliJ considers the master-project folder as the root for your gradle project and uses fully qualified task names for the test invocation. E.g.:common-core:cleanTest is a fully qualified task path for master-project gradle root project. However, gradle doesn't allow to use rootProject.name in the root project tasks path and since you have common-core/settings.gradle file you get the gradle error: Project 'common-core' not found in root project 'common-core'.

If you really need these multiple settings.gradle files, you can try to use flat structure (includeFlat in master-project)

0

We have individual child projects including one another, so we need multiple settings.gradle files.  Still we have some users who are not having this issue.  With this exact same project, many of our devs are able to run Unit tests without this problem.  The funny thing is that if I run code from a main() by right mouse clicking and selecting run inside common-core, it works fine.  The issue is for only some of us and only with Unit tests.  This makes me think, it may be a setting in IntelliJ.  Any idea why this would apply only to JUnit testing?

0

Figured it out.  Was just an issue with the Test Runner I had selected under Preferences.  I had it set to "Gradle Test Runner".  When I changed it to "Platform Test Runner", running unit tests from within the IDE started working.  Thanks for your input...

 

0

Please sign in to leave a comment.