No tests found with test runner 'JUnit 4'

扶醉桌前 提交于 2019-11-26 06:05:51

问题


My Java test worked well from Eclipse. But now, when I relaunch test from the run menu, I get the following message:

No tests found with test runner \'JUnit 4\'

In the .classpath file I have all jar files, and at the end have:

<classpathentry exported=\"true\" kind=\"con\" path=\"org.eclipse.jdt.junit.JUNIT_CONTAINER/4\"/>
    <classpathentry kind=\"output\" path=\"bin\"/>
</classpath>

How can I resolve this error and get tests running again?


回答1:


this just happened to me. Rebuilding or restarting Eclipse didn't help.

I solved it by renaming one of the test methods to start with "test..." (JUnit3 style) and then all tests are found. I renamed it back to what it was previously, and it still works.




回答2:


When we get these errors it seems like Eclipse is just confused. Restart Eclipse, refresh the project, clean it, let Eclipse rebuild it, and try again. Most times that works like a charm.




回答3:


In context menu of your 'test' directory choose 'Build path' -> 'Use as a source folder'. Eclipse should see your unitTests.java files as a source files. Warning 'No JUnit tests found' occures because there is no unitTests.class files in your 'build' directory




回答4:


Check if your test class extends "TestCase". if so, remove that clause. Your class does not need to extend from "TestCase" class. It's most of the cases I've met.

public class MyTestCase extends TestCase{
  @Test
  public void checkSomething() {
    //...
  }
}
//Result> AssertionFailedError: No test Found in MyTestCase

Following TestCase should be fine.

public class MyTestCase {
  @Test
  public void checkSomething() {
    //...
  }
}
//Works fine



回答5:


I was facing the same problem and I debugged it to bad examples on the web and internals of junit. Basically don't make your class extend TestCase as some examples show for Junit 4.x. Use some naming convention Test or if you want to have an annotation you can use @RunWith(JUnit4.class).

If you need access to assert methods extend Assert or use static imports.

If your class extends TestCase then even if you use Junit 4 Runner it will be run as 3. This is because in the initialization code there is detection:

See JUnit3Builder and the lines:

boolean isPre4Test(Class<?> testClass) {
    return junit.framework.TestCase.class.isAssignableFrom(testClass);
}

This returns true and the test for junit4 compatibility won't be tried.




回答6:


Try Adding

@Test above the method for the test like this

@Test
public void testParse()
{

}



回答7:


Yet another possible solution I'll throw into the ring: I wasn't able to run the test class either from the editor window, nor the Package Explorer, but right-clicking on the class name in the Outline view and selecting Run As JUnit Test did work... Go figure!




回答8:


No testsuits in JUnit4. Use annotations instead or use old JUnit3 name conventions.

Example:

@RunWith(Suite.class)
@SuiteClasses({YourClassWithTests.class})



回答9:


This happened to me as well. I tried restarting Eclipse and also prefixed my test-methods with tests. Neither worked.

The following step worked: Change all your test methods present in @BeforeClass and @AfterClass to static methods.

i.e. if you have your test method in the below format:

@BeforeClass
public void testBeforeClass(){
}

then change it to:

@BeforeClass
public static void testBeforeClass(){
}

This worked for me.




回答10:


I have found out the answer:

I got this error when I executed the test standalone from eclipse (right click on the method and choose to run as junit test),

When I executed the complete class as junit test the test executed correctly with the parameters.




回答11:


When I face this problem I just edit the file and save it... works like charm




回答12:


My problem was that declaration import org.junit.Test; has disappeared (or wasn't added?). After adding it, I had to remove another import declaration (Eclipse'll hint you which one) and everything began to work again.




回答13:


Very late but what solved the problem for me was that my test method names all started with captial letters: "public void Test". Making the t lower case worked.




回答14:


I tried the solution from Germán. It worked for all the method from my class but i have a lot of classes in my project.

So I tried removing from build path and then re-adding it. It worked perfectly.

Hope it helps.




回答15:


May be your JUnit launch configuration was for a individual test class, and you somehow changed that config to "run all tests in a source folder, package or project"

But that could trigger the "No tests found with test runner 'JUnit 4'" error message.

Or you did a modification in your test class, removing the @Test annotation.
See this wiki page.




回答16:


I also faced the same issue while running JUnit test. I resolved this by putting the annotation @Test just above the main test function.




回答17:


What fixed my case was similar to @JamesG's answer: I restarted Eclipse, rebuilt the project, and refreshed it; BUT before I did any of that, I 1st closed the project (right-click project in package explorer -> Close Project) and then re-opened it. Then it worked.

A workaround solution I found before finding that ultimate solution I just described: Copy the test class, and run the test class as JUnit.




回答18:


Check if the folder your tests are in is a source folder. If not - right click and use as source folder.




回答19:


Close and open the project worked for me.




回答20:


There is another chance, you might have changed Junit Test from lower version(e.g. Junit 3) to Junit 4 . Is so follow below steps:-

1. Right Click on class
2. Select Run as >> "Run Configurations"
3. Check your "Test Runner" option in new window
4. If it not same as maven change it for example change it as Junit 4.



回答21:


Six years later ... and there are still problems with Eclipse and occasionally not finding JUnits.

In my Eclipse Mars 2 I discovered that it won't recognise test classes pulled in from git if there are more than 9 or 10 @Test annotations in the file. I need to comment out any extra tests, run the test class, then uncomment them and re-run the class. Go figure...




回答22:


Is your Eclipse project maven based? If so, you may need to update the m2eclipse version.

Just a quick note: I have a project in Eclipse which is maven-based, and generated initially using the "new maven project" wizard in Eclipse. I'm using JUnit 4.5 for the unit tests, and could quite happily run the tests from the command line using maven, and individual tests from Eclipse using run as JUnit test.... However, when I tried to run all of the tests in the project by invoking run as JUnit test... on the project root node, Eclipse complained "no tests found with test runner junit 4". Solved by upgrading m2eclipse to the latest stable development build from the m2eclipse update site (specifically, I upgraded from version 0.9.8.200905041414 to version 0.9.9.200907201116 in Eclipse Galileo).

From here: http://nuin.blogspot.com/2009/07/m2eclipse-and-junit4-no-tests-found.html




回答23:


This happened to me too. I found that in Eclipse I didn't make a new Java Class file and so that's why it wasn't compiling. Try copying your code into a java class file if it's not already there and then compile.




回答24:


I found out that Eclipse seems to only perform JUnit 3 style tests if your test-class extends from TestCase. If you remove the inheritance, the annotations worked for me.

Beware that you need to statically import all the required assert* methods like import static org.junit.Assert.*.




回答25:


I had to do a mvn clean on command line then project->clean in eclipse. I renamed the class beforehand then renamed it back but i doubt that helped.




回答26:


I'm also running Eclipse with Maven (m2e 1.4). The tests were running with Maven, but not with Eclipse... even after several application of Maven>Update project.

My solution was to add some lines in the .classpath generated by m2e. The lines are now sticking.

<classpathentry kind="src" output="target/test-classes" path="src/test/java">
  <attributes>
    <attribute name="optional" value="true"/>
    <attribute name="maven.pomderived" value="true"/>
  </attributes>
</classpathentry>



回答27:


I have this problem from time to time. The thing that resolves the issue most for me is to run the JUnit test from Run configurations... ensuring that JUnit 4 is set as the test runner.

Generally, I see this issue when attempting to Run As... Junit test from the context menu on the Package Explorer. If you right click the code for the test you are trying to run and instead of selecting Run As... Junit Test you select Run configurations... ensure the Project, Test Class and test runner are set correctly, clicking apply, then run works all the time for me.




回答28:


I started to work with Selenium and Eclipse in my job and I was doing my first automated test and I deleted from the code @Before, @Test, and @After notes and I was having this issue "No tests found with test runner junit4".

My solution it was simply to add again the @Before, @Test and @After notes and with that my script worked. Is important to not delete this from the code.

This is a simple test that uses Google to search something:

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class TestingClass {

    private WebDriver driver;
    //Creates an instance of the FirefoxDriver
    **@Before**
    public void SetUp() throws Exception {
        driver = new FirefoxDriver();
    }

    **@Test**   
    //Search using keyword through Google Search
    public void TestTestClass2 () throws Exception {
        driver.get("http://www.google.com.mx/");
        driver.findElement(By.name("q")).sendKeys("selenium");
        Thread.sleep(10000);
        driver.findElement(By.name("btnG")).click();
        Thread.sleep(10000);
    }

    //Kill all the WebDriver instances
    **@After**
    public void TearDown() throws Exception {
        driver.quit();
    }

}



回答29:


Using ScalaIDE (3.0.4-2.11-20140723-2253-Typesafe) I was having a similar problem with the Right Click Scala Test Class-> Run As -> Scala Junit Test context menu.

I tried editting the class(but not for a compile failure), cleaning, closing the project, closing Eclipse. None of those worked to restore the context menu for classes that had previously worked well. The test classes don't use the @Test annotation and instead use the @RunWith(classOf[JUnitRunner]) annotation at the top of the class using ScalaTest code.

When I tried choosing Scala Junit Test from the Run Configuration launch editor directly, I received the dialog from the question. Footix29's answer was the key for me.

I noticed that even though I had cleaned my project a few times, my classes in the /bin directory hadn't actually been rebuilt in a while.

Here's how I got back the context menu, and was able to once again run Scala Junit Tests:

  • manually cleaned the classes by deleting the /bin/<package dir>* via Explorer
  • Project -> Cleaned the project along with a full rebuild

I suspect that a class edit in general is able to clean some saved state of Eclipse and get it going again. In my case all of the previously working classes I tried had failed so the manual clean step was just the hammer I needed. However, other tricks that affect Eclipse's concept of the class path/build state should also work.

Further, I think that this behaviour was triggered in part by attempting to refactor a Scala class by renaming it( which the Scala Eclipse IDE sucks at ), where all the cleanup after the initial file change is manual. There were no build errors, but there were also no warnings that I was expecting meaning something was definitely stuck in Eclipse's build state info.




回答30:


Right Click the Project -> Build Dependencies -> remove the ones which have been excluded from the build path -> Click OK

Right Click the Project -> Maven -> Update Project.

You should be good to go..



来源:https://stackoverflow.com/questions/2332832/no-tests-found-with-test-runner-junit-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!