No tests found with test runner 'JUnit 4'

257,287

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

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

Solution 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

Solution 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

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

Share:
257,287

Related videos on Youtube

user281070
Author by

user281070

Updated on July 08, 2022

Comments

  • user281070
    user281070 almost 2 years

    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?

    • phtrivier
      phtrivier about 14 years
      Can you post the content of your test (or at least the class declaration). Also you wrote "at the end have :" with nothing else.
    • prasad vsv
      prasad vsv about 9 years
    • Anand Rockzz
      Anand Rockzz about 6 years
      For junit 5 see here
  • user281070
    user281070 about 14 years
    I do not understand, I do not think that it is related to eclispe version , my test worked yesterday and not the case now because of Junit launch popup is there a way to restet to my first configuration ?
  • Aubrey
    Aubrey about 14 years
    Lamisse, I'm confused too. I suggest that you update your question to explain when the unit test run works and when it does not work.
  • user281070
    user281070 about 14 years
    @Test is here, the message is : could not faind main class toto.lolo.testrunner (which is specified in the run configuration )
  • VonC
    VonC about 14 years
    @lamisse: so, some kind of classpath issue? or a syntex issue: it should be toto.lolo.Testrunner and not toto.lolo.testrunner ('T')
  • phtrivier
    phtrivier about 14 years
    @lamisse : is "toto.lolo.Testrunner" the actual name of your TestRunner class ? Is it something you wrote ? Is the project / jar containing it in the classpath of your launch configuration ?
  • Thien
    Thien over 13 years
    Yep worked for me too and agree with dlamblin. Good reminder to always try restarting for any odd problem that can't be solved in the first 5 minutes of prodding.
  • Alex Kilpatrick
    Alex Kilpatrick over 13 years
    This solved the problem for me. If you are looking at the older examples of how to use Junit first, they will lead you astray.
  • metamatt
    metamatt about 13 years
    I had this problem, and I closed the source file where I'd defined my test class, double-clicked on that file in the navigator, and it worked. So closing and reopening the file worked, without restarting Eclipse.
  • Seth M.
    Seth M. about 13 years
    this really makes me want to look for a new IDE. this worked for me.
  • Codeman
    Codeman almost 13 years
    this worked for me, as well. For a bit of extra confirmation.
  • Rachel
    Rachel almost 12 years
    i have method like testQuotes along with other methods that do not start with test but still i get No tests found error
  • Andree
    Andree almost 12 years
    This is the solution that works for me. Restarting doesn't work.
  • quux00
    quux00 over 11 years
    This is what I was after since I have to have the "test" folder not in the "src" folder. I did this, then open/closed the project as suggested by the other answers and now it compiles and runs as a JUnit test. Thanks!
  • Littm
    Littm over 11 years
    Hi mate and welcome to Stackoverflow! :) Could you please elaborate your answer?
  • Martin Charlesworth
    Martin Charlesworth about 11 years
    closing and reopeneing the project fixed it for me!
  • MrDrews
    MrDrews about 11 years
    I had the same problem with a class that extends junit.framework.TestCase, and my methods needed to begin with "test". Rather than extending TestCase, I added an import: import static org.junit.Assert.*;, and I can name my methods how I want. Bizarre...
  • michael_s
    michael_s almost 11 years
    It seems even worse with Kepler than with Juno... - strangely clean build doesn't help at all, only explicitly changing the file helped (e.g. by inserting and removing a space and then saving). Afterwards I can "Run As-JUnit Test" and it works. So - not the renaming but simply changing the file did the trick for me.
  • BudsNanKis
    BudsNanKis over 10 years
    Version: Kepler Release Build id: 20130614-0229 It works for this release. But, if I renamed the test back to its original, it would not work. Looks like it needs the word "test" at the beginning for some reason.
  • Peter Clause
    Peter Clause about 10 years
    I did the same by selecting the project -> Java build path -> Source -> add the test folder. Many thanks!
  • Cassian
    Cassian over 9 years
    @Mahesh You mean refresh the file (select it and press F5)
  • Dio Phung
    Dio Phung over 9 years
    It is indeed the correct answer. I tested it with IntelliJ and JUnit 4.1 and it works !
  • Erick G. Hagstrom
    Erick G. Hagstrom about 9 years
    It seems that any edit of the file produces this result.
  • Brian Shotola
    Brian Shotola about 9 years
    Seriously, even if a test is annotated with @Test, the test function name still needs to be prefixed with "test"?
  • Brian White
    Brian White almost 9 years
    You can add 2015 and Eclipse Luna to that list, too. Actually a great many of my @Test methods still start with the prefix "test". I removed that prefix on one test, saved, and then all tests were found. Afterwards I added it back and all tests continued to be found. It seems it's more about changing a method (file?) such that Eclipse re-discovers everything.
  • Nicolas Albert
    Nicolas Albert over 8 years
    In 2016 too, with the AndroidJUnitRunner !
  • s1mm0t
    s1mm0t over 8 years
    2016 regardless of AndroidJUnitRunner!
  • randombee
    randombee about 8 years
    This is still valid, for anyone that comes here wondering if it works. I'm using Eclipse Mars.
  • Blueriver
    Blueriver almost 8 years
    Android Studio 2.1.1, May 2016, still works. I wonder if this answer will work until the end of the universe.
  • Babu
    Babu over 7 years
    Love this hack... August 2016
  • tryingToLearn
    tryingToLearn over 7 years
    The problem that I am facing is that, in eclipse my JUnit tests run fine, but when I export them to a JAR, I get the above error. Any ideas?
  • Vortex
    Vortex over 7 years
    Can't agree more with last posters. Still true in 2016
  • Powerslave
    Powerslave over 7 years
    Hahahaha, this is nuts! I'm on Mars.2 - unfortunately, this didn't help me. It keeps telling me nothing is there. UPDATE: I undid the renaming and BOOM! It sprung to life. On Oct 21, 2016
  • Veaceslav Gaidarji
    Veaceslav Gaidarji over 7 years
    "If your class extends TestCase then even if you use Junit 4 Runner it will be run as 3" - this is not true (at least for Groovy and Intellij IDEA). I'm using JUnit 4.12, and I have annotated the class with @RunWith(JUnit4) and extended from "GroovyTestCase". Without @RunWith(JUnit4) I face same issues.
  • Kamil Witkowski
    Kamil Witkowski about 7 years
    Remember to rename it to 'test(..)' not 'Test(...)' in my case it makes difference.
  • sharmaap
    sharmaap about 7 years
    The behavior is still the same as of 2017 (JUnit 4.12).
  • arc
    arc about 7 years
    Thanks! This fixed my issue too! May 2017
  • geneSummons
    geneSummons over 6 years
    If src/test/groovy is already on your build path as a source folder, remove it and then add it again. Easier than removing and replacing @Test in every test file...
  • Alanight
    Alanight almost 6 years
    Thanks, this solves my problem, but after renaming back, the same problem raise again.
  • MPSL
    MPSL about 5 years
    same, from the outline view it works, everything else does not - i have tried every solution posted in 3 stack overflow threads, nothing works
  • rich
    rich almost 5 years
    This works for me too, but is useless for testing coverage across a project.
  • tObi
    tObi almost 5 years
    This helped me in 2019 with STS 3.9.8... are you f*n serious?
  • frva
    frva over 4 years
    With Netbeans 8.02 this still happened, I wanted a very simple run that I've named "public void testIt()" => Netbeans returned no tests found... By renaming to "public void doASimpleTest()" this has worked.
  • Heinzlmaen
    Heinzlmaen over 4 years
    Is it the other way around these days? Adding "extends TestCase" solved the problem for me.
  • 01000001
    01000001 almost 4 years
    This answer here is helpful, I didn't have exactly same situation, but test methods names were quite similar and eclipse had issue finding them. Like testCase01, testCare02... I had to comment other "testCasexx" methods in order to run testCase01 or else it throws an exception. Making all test methods PUBLIC seem to have resolved this issue!!