Reputation: 1351
My Java tests worked well from Eclipse. But now, when I relaunch tests 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 I 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?
Upvotes: 120
Views: 299849
Reputation: 721
I had to make the class public. Before I had 'class myTests' and making it 'public class myTests' allowed intelliJ to find the tests.
Upvotes: 0
Reputation: 125
It might be silly, but I got the error making my @Test function private instead of public... too early in the morning.
Upvotes: 0
Reputation: 847
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!
Upvotes: 0
Reputation: 622
I tried all of the suggestions above. Only deleting existing test run configurations for that class finally resolved it. Eclipse is weird...
Upvotes: 0
Reputation: 3274
Using JUnit4 and a Gradle-based project, none of the earlier suggestions worked in my case. What did work was:
test
source folder, and click Properties
Run/Debug Settings
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.
Upvotes: 0
Reputation: 348
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.
Upvotes: 1
Reputation: 825
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.
Upvotes: 0
Reputation: 2635
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.
Upvotes: 0
Reputation: 972
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.
Upvotes: 0
Reputation: 41
You might be able to solve this simply by renaming your test class to have a name that ends with Test, e.g. ThisAndThatTest
Upvotes: 0
Reputation: 409
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
Upvotes: 0
Reputation: 20966
Add @Test
on top of your test.
Mouse hover to the annotation.
Chose 'add junit 4 library to classpath'
Upvotes: 1
Reputation: 1668
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
Upvotes: 0
Reputation: 19404
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
Upvotes: 1
Reputation: 1147
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
Upvotes: 0
Reputation: 6658
If you are trying to setup junit 5
add @RunWith(JUnitPlatform.class)
and import org.junit.platform.runner.JUnitPlatform;
so that eclipse can recognize the junit 5
test classes as test classes
Upvotes: -1
Reputation: 150
I also faced this Issue while implementing @RunWith(Cucumber.class). Since this annotation was inside a Public class and hence Eclipse Shows this "No tests found with test runner not found" error.
Once I called / placed the test runner annotation before the runner class, The tests showed up nicely.
Readers must identify which Junit Test Runner they are using. One can check with @RunWith(JUnit4.class) is Outside or inside the runner.class parenthesis {}. If its inside, Please place it outside.
Upvotes: -1
Reputation: 9
There are two reasons for this Exception:
For reason one, it has no leg to stand. Because like many people said: the ‘old’ methods work perfectly. Only the new-added methods cannot been identified. Then the second reason became the only explanation: JUnit can NOT find the freshly compiled classes. Having that in mind, I did this: Right click the project ->Java Build Path -> Edit Source item: project_name/src/test/java’s Output folder, change the Output folder into a ‘Specific output folder(path relative to project_name). Provide this value: target/test-class. Be hold: This is the default test source codes’ output folder defined by Maven. It has been overwrote by M2E (or me). That’s why JUnit cannot find it. So my guess is many people who had run into this lovely exception are most likely dealing with a Web project which is managed by Maven. Am I right?! This also contributed my conclusion that reason one should be excluded -- “old” methods can be found is because they had been automatically compiled when the Tomcat was (re)started. Into the ‘wrong’ path maybe, but JUnit found it anyway.
Before I had figured this out. I tried many things as advised: update to the latest JUnit4, Back and forth between build path and use as source folder, capital Test vs test, etc. Nothing happened until I specifically identified the output location for the test source. With all those explanation, my second reason should be re-phrased as this: the new codes had not been timely compiled to the right path! Good luck.
Upvotes: -1
Reputation: 539
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...
Upvotes: 2
Reputation: 1883
You can fix this issue by do as following:
<classpathentry kind="src"
path="src/test/java"/>
. You replace "src/test/java" by your test packageThis issue happened because of junit cannot recognize your source code :D
Upvotes: 1
Reputation: 7768
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..
Upvotes: 1
Reputation: 3961
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.
Upvotes: 1
Reputation: 2504
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 Test
s:
manually
cleaned the classes by deleting the /bin/<package dir>*
via ExplorerProject -> Clean
ed the project along with a full rebuildI 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.
Upvotes: 0
Reputation: 1
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();
}
}
Upvotes: 0
Reputation: 2849
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!
Upvotes: 6
Reputation: 3668
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.
Upvotes: 0
Reputation: 61
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.
Upvotes: 6
Reputation: 3365
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>
Upvotes: 0
Reputation: 1482
Check if the folder your tests are in is a source folder. If not - right click and use as source folder.
Upvotes: 1