简体   繁体   中英

Unable to run JUnit test in IntelliJ

I wanted to start project using TDD. Created the test directory and later changed it to package that is integrated with src direcotry. In both cases I get the same error:

Class not found: "tests.objectsTest"

I tried different techniques of importing JUnit jar and none solved problem. Also I tried to rename my test class but it gives no solutons whatsoever.

It seems that IntelliJ or JUnit changes name of the test class. Shouldn't it be objectsTest.tests ?

I am using JUnit version 4.12 and latest IntelliJ EAP.

This is my project structure:

Project:

-.idea
-src
  -logic
  -objects
-tests
  -test
   -test.java

src and tests are directories marked as Source and Test. Every package except test is empty. On my other PC with IntelliJ Community Edition everything works fine but on EAP there is this bug. Unfortunatelly I have to use EAP.

test.java code:

package test;

import org.junit.Test;

public class test {

    @Test
    public void canCreateInhabitant(){

    }
}

Have you checked if you have the JUnit plugin enabled? I (foolishly, shame!) disabled it at some point and was unable to get IDEA to run my tests until I remembered to turn the plugin back on...

Check the root directory of your classes. It must be marked as source (for java classes) or test (for java test classes).

It seems that your directory is not well marked in IntelliJ.

I did a simple test and put it on github .

It's the absolute simplest of tests but it works great, standing inside the test class pressing shift + ctrl + t will run the test.

Go ahead and clone it and try it out.

Easiest way is:

  • Open Class in Intellij and press Ctrl+Shift+T
  • Select "Create New Test"
  • Now, a new pop up will be opened where you can select Unit Test Library (For your case its Junit4)
  • Select the methods which you want to include in test
  • And there you go !

Sometimes I find that this happens when I try and launch "All tests" from the project folder in the structure view. Launching all tests by right clicking on the test root folder and selecting "all tests" from there seems to solve it.

Just had this happen to me. When I built via Maven it had a problem. When I fixed the problem, it would run the junits again. Goofy.

我遇到了同样的问题,我通过单击 File-> Invalidate chaces 和 Invalidate an Restart 解决了这个问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM