No tests found with test runner 'JUnit 4'

Viewed 264750

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?

47 Answers

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.

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

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.

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

Example:

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

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.

I've had issues with this recently, which seem to be fixed in the latest Eclipse.

eclipse-java 4.11.0,2019-03:R -> 4.12.0,2019-06:R

Add @Test on top of your test.
Mouse hover to the annotation.
Chose 'add junit 4 library to classpath'

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

My problem was using **

testng

** package org.testng.Assert;

You can replace it in libraries of the project (Maven) with JUnit and then need to refactor project.
OR add TestNG Eclipse plug-in

http://testng.org/doc/eclipse.html

I had this issue when my Java class name was similar to the Test class name. Just updated the name of the Test class by post fixing it with keywork "Test" and it started working

Before: Java class : ACMEController Test class : ACMEController

After: Java class : ACMEController Test class : ACMEControllerTest

In my case the issue was the generics parameters:

public class TestClass<T extends Serializable> {

removing :

<T extends Serializable>

and the option was available again.

I'm using Junit4

You might be able to solve this simply by renaming your test class to have a name that ends with Test, e.g. ThisAndThatTest

Check for updates! Eclipse is no doubt fully aware of this problem considering how many workarounds have been presented. There is obviously something systemic involved...something that defies explanation or any sense of consistency. It would have been useful if Eclipse would have let the community know what the problem was and how it was fixed.

Another recent find for me was to delete any source directories that had no files or the all files were excluded.

If you're sure it used to work and now it's not working anymore, chances are that the Java index got corrupted. In the preferences, search for "java index"; it's in "Java" and then there's a button "Rebuild Index" (or use Ctrl+3, "Find Actions", to quickly search for "Rebuild Java Index"). Once selected, the next time you try to run the JUnit test you should see that Eclipse is rebuilding the Java index and the JUnit tests should start now.

I ran into this issue because I had a @BeforeClass that was not static.

For some reason the error wasn't exposed through my normal junit run configuration.

I tried all the answers above.

In my case my project had both junit4 and junit5 dependencies. I was trying to use junit4 so after making sure I used correct junit4 imports it still did not work.

I finally found the problem. The eclipse JUnit runner used in the debug configuration was set to JUnit5. I changed it to JUnit4 and the tests are now found and executed.

Using JUnit4 and a Gradle-based project, none of the earlier suggestions worked in my case. What did work was:

  1. Right click your test source folder, and click Properties
  2. Jump to Run/Debug Settings
  3. Select all of the run/debug configurations, and delete them.

My guess is that one of these launch configurations was messing up my JUnit4 runner. Resetting all of the run/debug configurations solved JUnit4 not finding any tests to run.

I tried all of the suggestions above. Only deleting existing test run configurations for that class finally resolved it. Eclipse is weird...

ok In my case (using Junit5), if i tried to run with

@RunWith(MockitoJUnitRunner::class)

I got error

No tests found in ServiceImplTest
Is the method annotated with @Test?
Is the method public?
org.mockito.exceptions.base.MockitoException: 

No tests found in ServiceImplTest
Is the method annotated with @Test?
Is the method public?

Solution: Simply removing @RunWith(MockitoJUnitRunner::class) from Top of class worked!

Related