68

I'm using JUnit since I started this project and everything works just fine. I have a couple of hundreds tests, and of course, here and there I start them all. Right click on root test folder, run (or debug) with JUnit. But since yesterday, when I do that, the result is:

Process finished with exit code 0

without starting any of tests. Same thing is with any folder in folder tree. I can still run a single test class, but I really need an option to start them all. If any of you have any ideas, please drop them here.

Here is what is logged when I try to run tests:

2012-10-31 15:16:55,693 [2727217]  ERROR - ij.psi.impl.source.PsiFileImpl - IntelliJ IDEA 11.1.2  Build #IU-117.418 
2012-10-31 15:16:55,693 [2727217]  ERROR - ij.psi.impl.source.PsiFileImpl - JDK: 1.6.0_31 
2012-10-31 15:16:55,693 [2727217]  ERROR - ij.psi.impl.source.PsiFileImpl - VM: Java HotSpot(TM) Client VM 
2012-10-31 15:16:55,693 [2727217]  ERROR - ij.psi.impl.source.PsiFileImpl - Vendor: Sun Microsystems Inc. 
2012-10-31 15:16:55,693 [2727217]  ERROR - ij.psi.impl.source.PsiFileImpl - OS: Windows 7 
2012-10-31 15:16:55,693 [2727217]  ERROR - ij.psi.impl.source.PsiFileImpl - Last Action: RunClass 
2012-10-31 15:16:55,693 [2727217]  ERROR - m.intellij.util.ExecutorsQuery - com.intellij.psi.tree.IFileElementType cannot be cast to com.intellij.psi.tree.IStubFileElementType 
java.lang.ClassCastException: com.intellij.psi.tree.IFileElementType cannot be cast to com.intellij.psi.tree.IStubFileElementType
    at com.intellij.psi.impl.source.PsiFileImpl.calcStubTree(PsiFileImpl.java:976)
    at com.intellij.psi.stubs.StubIndexImpl$1.perform(StubIndexImpl.java:239)
    at com.intellij.psi.stubs.StubIndexImpl$1.perform(StubIndexImpl.java:220)
    at com.intellij.util.indexing.ValueContainer.forEach(ValueContainer.java:60)
    at com.intellij.psi.stubs.StubIndexImpl.process(StubIndexImpl.java:220)
    at com.intellij.psi.stubs.StubIndexImpl.get(StubIndexImpl.java:194)
    at com.intellij.psi.stubs.AbstractStubIndex.get(AbstractStubIndex.java:33)
    at com.intellij.psi.impl.java.stubs.index.JavaAnnotationIndex.get(JavaAnnotationIndex.java:47)
    at com.intellij.psi.impl.search.AnnotatedElementsSearcher.a(AnnotatedElementsSearcher.java:93)
    at com.intellij.psi.impl.search.AnnotatedElementsSearcher.execute(AnnotatedElementsSearcher.java:44)
    at com.intellij.psi.impl.search.AnnotatedElementsSearcher.execute(AnnotatedElementsSearcher.java:28)
    at com.intellij.util.ExecutorsQuery.processResults(ExecutorsQuery.java:42)
    at com.intellij.util.AbstractQuery.forEach(AbstractQuery.java:67)
    at com.intellij.util.InstanceofQuery.forEach(InstanceofQuery.java:54)
    at com.intellij.execution.ConfigurationUtil.addAnnotatedMethodsAnSubclasses(ConfigurationUtil.java:97)
    at com.intellij.execution.ConfigurationUtil.findAllTestClasses(ConfigurationUtil.java:77)
    at com.intellij.execution.junit.TestPackage$MySearchForTestsTask.run(TestPackage.java:397)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$TaskRunnable.run(ProgressManagerImpl.java:469)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:218)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:169)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$8.run(ProgressManagerImpl.java:378)
    at com.intellij.openapi.application.impl.ApplicationImpl$6.run(ApplicationImpl.java:434)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
    at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:145)
5
  • Is your test folder still set for test (green color)? Have you disabled maven or made some changes with it?
    – Random42
    Oct 31, 2012 at 12:24
  • It is still green and every subfolders icon has a small green point on it. I made no changes with maven. Also, since yesterday, every time I restart IDEA, atlassian connector loose current configuration, and I need to add it again. Oct 31, 2012 at 13:04
  • I fix problem with atlassian connector, but still can't run tests from folder... Oct 31, 2012 at 13:54
  • Check idea.log for exceptions (Help | Reveal Log in ...). Try running IDEA with the default settings (rename its directories: devnet.jetbrains.net/docs/DOC-181).
    – CrazyCoder
    Oct 31, 2012 at 14:14
  • I added error log into the question. Oct 31, 2012 at 14:20

16 Answers 16

74

OK, I fixed it.

In menu, under File, there is an invalidate cache option. That fixed it!

5
  • 8
    This cache option is quite unpredictable workaround. hope IDEA developers will fix cases or automate them for us :) Nov 2, 2012 at 9:35
  • 10
    This is not working for me. :( is there any other solution ?
    – Paresh
    Mar 30, 2017 at 23:49
  • 3
    Come on, this is the most annoying aspect of IntelliJ ... searching for hours for a solution, and then this default answer ... similar to "reboot Windows".
    – Brain
    Sep 8, 2021 at 6:00
  • 4
    We should suggest Idea team to rename 'Invalidate cache' to 'Magically fix your problem' Mar 18, 2022 at 20:17
  • 2
    11 years later and this hasn't been fixed? Come on IntelliJ. Clear cache should be a last resort, not a permanent solution.
    – Michael
    Mar 8, 2023 at 14:22
53

On the latest IDE (as of Community Edition 2016.1) this option is more simplified.

right click on the folder you want to be recognized as a source folder:

Mark Directory As -> Sources Root

enter image description here

6
  • 2
    but if you have multimodule project, it's really pita to set it everywhere manually :(
    – chipiik
    Feb 20, 2017 at 15:31
  • It should be a Test Sources root, otherwise your project will compile the tests and the test libraries also for a release. Mar 23, 2018 at 18:23
  • 1
    I only have "excluded" there.
    – Line
    Mar 7, 2019 at 16:18
  • This should work right out of the box and no need to set that manually. But, in a few cases, I was required to do the same.
    – Arefe
    May 23, 2019 at 7:56
  • This is only solution that worked for me. I also marked a "test->java" as "Test Sources Root". Thanks. Jan 30, 2022 at 17:41
51

For some reason Intellij Idea don't recognize classes as Test class that are not explicitly public, in case class has default access specificator it will not be marked as a test. Try to change class to meet this rule. Example:

public class TestMe {
   @Test
   public void shouldTest(){
      Assert.assertEquals("test", "test");
   }
}
4
  • 1
    That solved my issue. I tend to mixed Python syntax with Java syntax, and forgot that classes in Java should use an access modifier.
    – mgilsn
    Jul 10, 2020 at 2:50
  • 1
    Test classes AND methods should be public. This fixed the issues for me
    – Tamb
    Mar 8, 2021 at 17:33
  • Thanks this fixed my issue as well! Both class and method need to be public.
    – gstanley
    Nov 7, 2022 at 15:38
  • hahahahahaha I added 'public' and the class was recognized, and then I removed 'public' and it keeps being recognizing LOL jesus... why? Feb 8, 2023 at 18:28
18

Just to add a different scenario, which happened to me and some of my coworkers:

Alt+enter in the class -> create test: IJ (2017.1.1) proposes Arquillian Junit as first option of testing library and by going with it the test class and methods are created without the 'public' identifier. Then if one decides to change the testing library to JUnit, it is easy to forget about the absence of the 'public' identifier, which causes the tests not to be recognised by IJ. Of course, the solution is to place the 'public' identifiers.

Although this is not what happened to you it has the same consequence, therefore this answer may help others experiencing the same symptoms.

1
  • To add to this: the maven surefire plugin uses strict naming conventions for test methods. For the right patters (e.g. *Test.java) see the Apache plugin page. Mar 26, 2021 at 16:54
5

Fixed this a few times by... open file --> project structure. There, look on the left side menu as last entry there is "problem" count(if more then zero). click on those and then for for each problem highlighted in red in intellij in modules/libraries, remove them (minus button). They will be given module names like "test1..23" since intellij cant make out the structure. After deleting all marked red, close + restart intellij. Dont run maven refresh at this stage, since it will reintroduce the same error again. By restarting intellij will trigger a new scan of the project enter image description herestructure again.

0
4

In my case, we are moving from JUnit 4 to JUnit 5 and I mixed the @Test of them.

In short:

  • if you use @RunWith() of JUnit 4, you must use @org.junit.Test to annotate test methods.
  • if you use @ExtendWith() of JUnit 5, you must use @org.junit.jupiter.api.Test instead.

When I change the import, my class can be run as test class again.

2
  • 1
    It happened again; then I find that I am using JUnit 4 in main source set, and JUnit 5 in test source set. Although their dependency scope is all correct in build.gradle, I can only get JUnit 5 tests running and had to remove JUnit 4 tests.
    – WesternGun
    Aug 30, 2020 at 0:36
  • 1
    Good point for to pay attention to. Tried all options mentioned in other solutions, including adding public to test class as proposed by @João Matos (which actually marked the class as a Test executable class, but tests still failed to run). So best way was copying one of the working tests from another non public test class into this class. The moment I did it, the @Test annotation was imported using fully qualified name which is different from all other @Test annotations. Feb 21, 2021 at 9:14
4

I ran into this issue with Ubuntu 20.04, Java 11.0.8-zulu, and Intellij 2020.2.

Upgrading Junit from 5.5.2 to 5.6.2 and Junit Platform from 1.5.2 to 1.6.2 solved it for me.

That seemed odd to me, so I switched the versions back, which now also works. I think some sort of cache invalidation is likely the best explanation, albeit the corresponding button for this didn't work for me.

So basically, what is said here https://stackoverflow.com/a/58687202/6476706 (I tried to comment there but can't...).

Anyway, still an issue in 2020, maybe this helps someone.

0
3

None of the previous answers worked for me in IDEA 2019.1.2 (or -.3) with Junit Jupiter 5.5.0. I had git-cloned a couple of projects that I had pushed to GitHub from a different computer, where their tests ran fine. On my new laptop, I got "No tests were found" no matter how I tried to run them.

What finally did work: I deleted the IDEA-generated test.iml file, because I had noticed there was no such file in a virgin project I had created just to see if I could run tests at all (I could indeed). After I deleted this file in both older projects, tests ran perfectly. No new test.iml was generated.

I don't know how or when those files got generated. Because the git repos are private, I didn't bother to gitignore IDEA's *.*ml files; nevertheless, they weren't in the GitHub repos. Furthermore, they were at the roots of the projects, not in the .idea directory.

0
2

For me it was that the class being tested was not public. Once I set it to public the green run test icons popped up. Note, this is only an issue in Junit 4.

1

In my case I had to:

  • close the project
  • open the build.gradle file
  • overwrite existing project? Yes
  • right click on project
    • Mark directory as > Resources Root
1

Have a try changing a JUnit dependency version. One release forward or backwards. It can help invalidate test cache.

If you use a dependency management tool such as Maven or Gradle, go to the .pom or .gradle file and change the version of the JUnit library.

For instance change 4.11 to 4.12 or 4.10 in the version section:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <!-- change here -->
      <version>4.11</version>
      <scope>test</scope>
   </dependency>
</dependencies>
1

I ended up manually categorising every sources root and test sources root in all the modules for the project. Worked like a charm.

1
  • Sure, in the IDE project pane on the left, expand the projects. For every folder under src/main/, e.g. java or kotlin sources, right click on the folder, and Choose Mark Directory as, Test Sources Root, Sources Root.. whichever is applicable.
    – ryuzaki
    Jan 28, 2021 at 13:14
0

If you are using IntelliJ for test execution and debugging, but not for editing. Your other editor/IDE may be battling with IntelliJ for control over generated files (*.class) -- resulting in IntelliJ reporting that No tests were found (and maybe spurious build errors too).

In my case, Visual Studio Code seem to be having this effect (after recent updates to both tools).

The solution that I have found is to close Visual Studio Code and then clean rebuild the project with IntelliJ or from the command-line. In otherwords, my dual-IDE workflow no longer works -- so I have to edit in IntelliJ.

0

A poor workaround for this is to let IntelliJ run tests instead of Gradle.

File > Settings > Build Tools > Gradle > Run Tests With > IntelliJ Test Runner

This is a similar issue with another test framework on Gradle. https://youtrack.jetbrains.com/issue/IDEA-221159

0

I had a similar problem. After spending some time I found that I have created my class itself under the test root :-) When I moved the class to the main root, everything worked fine for me :-)

1
0

I ended up with same problem and there are 3 steps I followed to resolve, sharing my solution so this may help someone.

  1. Make the test/Java folder as "Test Resources" folder.

enter image description here

  1. Open the java file in editor, place the cursor on file name in editor and do 'Alt + Enter', now select the option 'Creare Test', this will create a Test class just in where your java file is located. enter image description here enter image description here

  2. This step is not for all, in some cases, test file will be created just at java file location, in such scenarios, open the test file in editor, highlight one error and you see an option called 'move to test resource folder (not exactly sure of option name)', click on this option and you'll see that your file will be moved to test/Java path.

Note: Once 3rd step is done, then all your test files will be by default created in test resouce folder (test/Java) only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.