Curious Minds

Clear answers to science and history questions

IntelliJ Not Recognizing Tests in Test Folder? Fix It

· 6 min read · Science

IntelliJ IDEA most often stops recognizing tests in a test folder because the folder is no longer marked as a Test Sources Root, or because the project's module configuration, build tool sync, or SDK has changed. When the folder loses that designation, IntelliJ treats its files as ordinary source files, so test annotations such as @Test are not resolved and the run gutter icons disappear. The fix is usually to restore the test root designation and re-sync the project, but several related causes can produce the same symptom.

What “Test Sources Root” Actually Means in IntelliJ

IntelliJ IDEA does not decide that a file is a test by its folder name alone. It uses the module's content root configuration, where each source folder is assigned a type: Sources, Test Sources, Resources, Test Resources, or Excluded. Only files under a Test Sources root are compiled with the test classpath and indexed for test-related inspections. That is why a folder literally named test can still be ignored: the name is a convention, not the rule. The rule is the folder's assigned type in the module structure.

When the type is correct, IntelliJ resolves JUnit, TestNG, or Spock annotations, shows green run icons in the gutter, and includes the classes in the test run configuration. When the type is lost, those features silently stop working while the files themselves remain intact.

Common Triggers That Remove the Test Root Designation

Several routine events can reset or override the folder type. The most frequent are:

  • Build tool re-import or sync. Maven and Gradle define source sets in their own configuration. If the build file changes, or a sync runs with a different profile, IntelliJ may rebuild the module structure and lose manual folder markings.
  • Project file corruption or a fresh checkout. The .idea directory stores module settings. Deleting it, restoring from version control without it, or opening the project in a different IntelliJ version can reset source roots.
  • Module or SDK changes. Adding a module, changing the project SDK, or switching between JDKs can invalidate the configuration and leave test folders unmarked.
  • Folder moved or renamed. Moving a test folder outside the module content root, or renaming it, can detach it from its previous type.
  • Accidental “Unmark” action. The context menu on a folder includes Mark Directory as, and choosing Unmark or a different type removes test status.

How to Restore Test Recognition Step by Step

Work through these steps in order; most cases are resolved by the first two.

  1. In the Project view, right-click the test folder and choose Mark Directory as > Test Sources Root. The folder icon should change to a green test badge.
  2. Open File > Project Structure > Modules, select the module, and check the Sources tab. Confirm the test folder appears with the Tests type. If it is listed as Sources or unmarked, correct it there.
  3. If the project uses Maven or Gradle, run a full re-import or sync. In Maven, use the Maven tool window's Reload All Maven Projects. In Gradle, use the Gradle tool window's Refresh. Let the build tool regenerate the module structure.
  4. Check that the test framework library is on the test classpath. For Maven, the dependency should have <scope>test</scope>. For Gradle, it should be in testImplementation. Then verify the library appears under External Libraries.
  5. Invalidate caches if the folder type looks correct but annotations still do not resolve. Use File > Invalidate Caches / Restart and choose Invalidate and Restart.
  6. As a last resort, close the project, back up and delete the .idea directory, then reopen the project and let IntelliJ rebuild the configuration from the build files.

When the Folder Is Marked but Tests Still Are Not Found

If the test root is correctly set and the run icons are still missing, the problem is usually the test framework configuration rather than the folder. IntelliJ needs to know which framework to use. Check File > Project Structure > Modules > Dependencies for the JUnit or TestNG library, and confirm the correct version is present. A missing or mismatched framework version can prevent annotation resolution even when the folder type is right.

Another frequent cause is a language level mismatch. If the module's language level is lower than the Java version used by the test library, or the test sources use features the compiler does not accept, IntelliJ may fail to index them as tests. Check the Language Level in Project Structure and the compiler target in the build file.

Finally, confirm that the test class itself follows the framework's requirements. JUnit 4 requires @Test from org.junit.Test; JUnit 5 requires org.junit.jupiter.api.Test. Mixing imports from different JUnit versions is a common reason a class is not recognized as a test.

Maven and Gradle Specifics

Build tools own the source set layout. In Maven, the default test directory is src/test/java, and test resources live in src/test/resources. If a project uses a nonstandard layout, the build-helper-maven-plugin or a custom <testSourceDirectory> entry is needed; otherwise IntelliJ will not mark the folder automatically. In Gradle, the sourceSets block defines test directories. A custom path must be declared there, or IntelliJ will not know it is a test source set.

After editing either build file, always re-sync. Manual folder markings can be overwritten by the next sync if the build tool does not agree with them, which is why fixing the build configuration is more durable than marking the folder by hand.

Why the Problem Sometimes Appears Suddenly

The word “suddenly” is a clue. IntelliJ rarely forgets a test root on its own. The change usually follows an action: a build file edit, a version control update that changed the .idea files, a JDK switch, a plugin update, or a project reopened from a different location. Because the folder still exists and the code still compiles, the loss of test status is easy to miss until run icons vanish or annotations turn red. Checking recent changes to build files and project settings often identifies the trigger faster than inspecting the folder itself.

Frequently Asked Questions

Why does IntelliJ not see my test folder after I renamed it?

Renaming or moving a folder can detach it from its assigned type in the module structure. Right-click the folder and mark it as Test Sources Root again, then re-sync the build tool if one is used.

Do I need to mark the test folder manually if I use Maven or Gradle?

Usually not. The build tool declares test source sets, and IntelliJ applies them on import. Manual marking is only needed for nonstandard layouts or when the build tool configuration is incomplete.

Why are my JUnit 5 tests not recognized but JUnit 4 tests are?

The JUnit 5 library may be missing from the test classpath, or the imports may be mixed. Confirm that the JUnit Jupiter dependency is present with test scope and that the test class imports org.junit.jupiter.api.Test.

Does deleting the .idea folder fix test recognition?

It can, because it forces IntelliJ to rebuild module settings from the build files. Back up the folder first, since it also contains run configurations and other project settings.

Why do tests run from the command line but not from IntelliJ?

The build tool compiles and runs tests using its own configuration, while IntelliJ uses the module structure and run configuration. If the folder is not marked as a test root or the framework library is not on the test classpath, IntelliJ will not offer to run the tests even though the command line works.

Can invalidating caches remove test root markings?

Invalidating caches clears indexes and can reset some derived state, but it does not normally change module source root types. If markings disappear after invalidation, check whether a build tool sync ran at the same time and overwrote them.

In this article
  1. What “Test Sources Root” Actually Means in IntelliJ
  2. Common Triggers That Remove the Test Root Designation
  3. How to Restore Test Recognition Step by Step
  4. When the Folder Is Marked but Tests Still Are Not Found
  5. Maven and Gradle Specifics
  6. Why the Problem Sometimes Appears Suddenly
  7. Frequently Asked Questions