79

I'm evaluating IntelliJ (13.0.2 133.696) and cannot get jUnit tests to run from within the IDE.

My project is a multi module gradle project and uses scala.

Test class is located under src/test/scala/xxx/xxxxx/xxx/xxxx/xxxxx and everytime i try to run from IDE i get the same error:

Class not found: "xxx.xxxxx.xxx.xxxx.xxxxx.AccountRepositoryTest"

Test class is nothing fancy, simple jUnit test:

@RunWith(classOf[SpringJUnit4ClassRunner])
@ContextConfiguration(classes = Array(classOf[DataConfig], classOf[SettingsConfig]))
class AccountRepositoryTest extends AssertionsForJUnit {

I've found a related question Cannot run Junit tests from IDEA 13.0 IDE for imported gradle projects , but the provided fix (upgrade to 13.0.2) does not work.

I've even tried upgrading to the latest EAP, still the same issue.

Community
  • 1
  • 1
gerasalus
  • 7,538
  • 6
  • 44
  • 66

21 Answers21

66

I looked through some of these answers, fussed with Project Settings, tried a few things, etc. and nothing worked. (Full disclosure: I'm not trying to juggle Gradle here; I'm just using Maven, but I don't see what this has to do with Gradle.)

I'm using IDEA 14.

What I found to work, because it just simply seemed IntelliJ had lost its way, was this:

$ rm -rf .idea project-name.iml

Then relaunched IntelliJ and did File -> Open -> navigate to the root of my project, etc.--in short, just recreated my project.

IntelliJ got over it. I may have messed something up originally in this project as I had done plenty of refactoring both package- and class names and I had even changed the project name. (It was probably my fault it happened.)

Russ Bateman
  • 18,333
  • 14
  • 49
  • 65
  • 2
    Thanks, For me it was enough to just move *.iml files and do generate it again(in maven tab - had the same problem with maven) – fallens4e May 27 '16 at 07:13
  • 2
    I moved some modules around and it also broke the project structure in the .iml config files, resulting in similar symptoms. – Olivier Tonglet Apr 04 '17 at 09:32
  • This has the disadvantage of making me spend a great deal of time, because my projects tend to have many modules, in setting up (resetting up) the modules themselves any time I have to do this. It seems to happen any time I do much inter-module refactoring (moving). – Russ Bateman May 25 '17 at 17:18
  • 1
    Worked for me. My project has sub maven modules and instead of creating 1 rootmodule.iml it created an iml file for each sub maven module which caused the problem. To fix this I had to reimport the project. – Glenn Van Schil Dec 13 '17 at 15:46
  • You might need an additional ```find . -name "*.iml" -delete``` for larger projects. – ikaerom Jun 27 '20 at 07:43
36

I had this same problem, and in my case the problem was due to the "Project compiler output" path being left blank in Project Settings.

Project Structure configuration screen

To fix it I created a classes directory in my project root, and set Project compiler output to the absolute path (use the button to browse).

bradley.ayers
  • 37,165
  • 14
  • 93
  • 99
16

Go to Project Settings -> Project.

Fill in Project compiler output: ex. D:\repo\Project\out

Go to Module -> Paths

Make sure that:

output path is like D:\repo\Project\out\production

test output path like D:\repo\Project\out\test

Should work!

treefolk
  • 169
  • 1
  • 6
  • Just got the same issue on IntelliJ14. Setting used to be a relative directory, had to set an absolute one. Thanks! – louisgab Apr 12 '18 at 13:27
10

Simply 'Build > Rebuild Project' worked for me.

eleuteron
  • 1,863
  • 20
  • 26
7

Check Run/Debug configuration for that test "Use classpath and SDK of module:" should point into your module.

In meantime you module must have a Scala facet and that class must be inside the "Test source Folders".

Stas
  • 751
  • 1
  • 11
  • 22
6

You can try to invalidate the cache and restart. That usually will resolve issues when you add new dependencies / classes.

RvK
  • 234
  • 2
  • 6
5

Make sure your test class package and the class for which you are writing test case are not same. If both test case and the class is having the same package, the compiler will look in the src folder and ignores the test folder.

Jet
  • 3,018
  • 4
  • 33
  • 48
4

I had the same problem. I changed a path in Module Settings -> Modules -> Paths -> Test output path to my directory for test classes bytecode (exclude output paths on). Now everything works!

MaKri
  • 190
  • 1
  • 8
  • I am using Gradle 2.6 and forgot adding "apply plugin: 'scala'". After adding this, everything works fine for me. – Minh Do Jun 27 '16 at 04:36
4

IDEA restart solved the issue for me.

greene
  • 637
  • 6
  • 6
2

Just make shure the folder of your test file marked as a test folder in Intellij IDEA. That worked for me. If you have multiple directories with source files with the same name, add package to your class source file, if not present!

WebComer
  • 1,131
  • 2
  • 19
  • 31
1

I had the same problem, Intellij wasn't finding the Test output path. Running the regular application had no problems however.

For me, the fix was changing from inherited project compile paths, to using module compile output paths.

Project Settings -> Modules -> (Your module) -> Paths (tab)

Change the radio select button to "Use module compile output path". For me the autofilled suggestion worked, you may need to manually put in the correct Test Output Path if the autosuggestion doesn't work. Remember to apply the settings change.

gan
  • 3,068
  • 2
  • 19
  • 32
1

As of for gradle project X, deleting both:

  • X/build
  • X/out

and running tests again helped me resolve this issue.

El El
  • 27
  • 5
1

Try this in this order:

  • rebuild project (+strangely, select another app, reselect idea for context-switch, seems to force files reload ?!)
  • invalidate cache/restart idea
  • reimport project /create a new project
Shimbawa
  • 262
  • 3
  • 12
1

Just renaming the file to something different and back worked for me :)

Vadiraja K
  • 819
  • 8
  • 8
0

modify the content in tag of the module's .iml file just as the following. It works for me.

<content url="file://$MODULE_DIR$">
  <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
  <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
  <excludeFolder url="file://$MODULE_DIR$/target" />
</content>
0

Had the same problem, fixed it by recreating the project in a directory path that had no spaces, colons, periods or other special characters anywhere in the full path. Apparently IntelliJ can be finicky about the project path.

Clark
  • 174
  • 1
  • 5
0

My issue remained after building, clean and rebuild, closing and opening the project (using intellij), project compiler output was correct; in the end I just deleted the folder out from my project directory. Note that my issue was only on the newly added Junit calling a newly added method. The rest of Junits were working fine.

user666
  • 1,750
  • 3
  • 18
  • 34
0

For me it was that one test was failing and the error was misguiding.

So:

  1. I re-imported module in intellij as standalone project
  2. Run the test
  3. Fix test issues
  4. Run modules again and it started to work.
John Tribe
  • 1,407
  • 14
  • 26
0

tl;dr: Missing file extensions can cause this error.

details:

In my case the test classes were missing the .java extension. E.g. a file named UserTest instead of UserTest.java

  • Was hard to find, everything looked normal from within the IDE (apparently IntelliJ rather uses the file contents to display a corresponding symbol).
  • Was not an issue as long as I used mvn from command line (with the surfire plugin enabled in pom.xml) or a dedicated maven launcher configuration, but caused the initial error message when launching using IntelliJs test or coverage menu / buttons.
  • Everything working as expected as once I added the missing file endings.
m5c
  • 45
  • 7
0

The root cause might be hidden in Java Compiler settings, it was so for me at least. In particular, feel free to adjust those settings using some hints from another thread.

Maksym Kosenko
  • 515
  • 7
  • 12
0

After trying most of the suggestions above with no success, I noticed that the 'generated' test class was created without a public declaration. No idea why but changing this (adding public to the class declaration) made it all work.
For reference this is a Java, Maven project