No tests found with test runner 'JUnit 4'
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.
Related videos on Youtube
user281070
Updated on July 08, 2022Comments
-
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 alljar
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 about 14 yearsCan 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 about 9 yearscheck this one.. it should help stackoverflow.com/questions/22469480/junit-no-tests-found
-
Anand Rockzz about 6 yearsFor junit 5 see here
-
-
user281070 about 14 yearsI 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 about 14 yearsLamisse, 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 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 about 14 years@lamisse: so, some kind of classpath issue? or a syntex issue: it should be
toto.lolo.Testrunner
and nottoto.lolo.testrunner
('T') -
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 over 13 yearsYep 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 over 13 yearsThis 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 about 13 yearsI 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. about 13 yearsthis really makes me want to look for a new IDE. this worked for me.
-
Codeman almost 13 yearsthis worked for me, as well. For a bit of extra confirmation.
-
Rachel almost 12 yearsi have method like
testQuotes
along with other methods that do not start with test but still i get No tests found error -
Andree almost 12 yearsThis is the solution that works for me. Restarting doesn't work.
-
quux00 over 11 yearsThis 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 over 11 yearsHi mate and welcome to Stackoverflow! :) Could you please elaborate your answer?
-
Martin Charlesworth about 11 yearsclosing and reopeneing the project fixed it for me!
-
MrDrews about 11 yearsI had the same problem with a class that extends
junit.framework.TestCase
, and my methods needed to begin with "test". Rather than extendingTestCase
, I added an import:import static org.junit.Assert.*;
, and I can name my methods how I want. Bizarre... -
michael_s almost 11 yearsIt 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 over 10 yearsVersion: 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 about 10 yearsI did the same by selecting the project -> Java build path -> Source -> add the test folder. Many thanks!
-
Cassian over 9 years@Mahesh You mean refresh the file (select it and press F5)
-
Dio Phung over 9 yearsIt is indeed the correct answer. I tested it with IntelliJ and JUnit 4.1 and it works !
-
Erick G. Hagstrom about 9 yearsIt seems that any edit of the file produces this result.
-
Brian Shotola about 9 yearsSeriously, even if a test is annotated with @Test, the test function name still needs to be prefixed with "test"?
-
Brian White almost 9 yearsYou 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 over 8 yearsIn 2016 too, with the AndroidJUnitRunner !
-
s1mm0t over 8 years2016 regardless of AndroidJUnitRunner!
-
randombee about 8 yearsThis is still valid, for anyone that comes here wondering if it works. I'm using Eclipse Mars.
-
Blueriver almost 8 yearsAndroid Studio 2.1.1, May 2016, still works. I wonder if this answer will work until the end of the universe.
-
Babu over 7 yearsLove this hack... August 2016
-
tryingToLearn over 7 yearsThe 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 over 7 yearsCan't agree more with last posters. Still true in 2016
-
Powerslave over 7 yearsHahahaha, 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 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 about 7 yearsRemember to rename it to 'test(..)' not 'Test(...)' in my case it makes difference.
-
sharmaap about 7 yearsThe behavior is still the same as of 2017 (JUnit 4.12).
-
arc about 7 yearsThanks! This fixed my issue too! May 2017
-
geneSummons over 6 yearsIf 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 almost 6 yearsThanks, this solves my problem, but after renaming back, the same problem raise again.
-
MPSL about 5 yearssame, from the outline view it works, everything else does not - i have tried every solution posted in 3 stack overflow threads, nothing works
-
rich almost 5 yearsThis works for me too, but is useless for testing coverage across a project.
-
tObi almost 5 yearsThis helped me in 2019 with STS 3.9.8... are you f*n serious?
-
frva over 4 yearsWith 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 over 4 yearsIs it the other way around these days? Adding "extends TestCase" solved the problem for me.
-
01000001 almost 4 yearsThis 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!!