Running tests on Intellij: Class not found

2019-01-23 10:47发布

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.

9条回答
手持菜刀,她持情操
2楼-- · 2019-01-23 11:03

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-23 11:04

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).

查看更多
Emotional °昔
4楼-- · 2019-01-23 11:04

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.

查看更多
Melony?
5楼-- · 2019-01-23 11:07

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.)

查看更多
Melony?
6楼-- · 2019-01-23 11:11

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".

查看更多
Rolldiameter
7楼-- · 2019-01-23 11:15

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!

查看更多
登录 后发表回答