问题
I created a JUnit 4 test in Eclipse by right-clicking on a Java class and selecting New JUnit Test Case. When I right-click the test class I get "Run on Server", but not "Run as JUnit Test". I am using Eclipse 3.6.1.
回答1:
In my case, Eclipse must have reached a corrupt state. Restarting Eclipse fixed the problem.
回答2:
I think I see the problem. You need to have an actual test in the file before Eclipse identifies it as a test case. Try inserting the following:
@Test
public void foo() {
}
回答3:
Make sure your class has JUnit traits (extends from
TestCase
, or use@Test
etc);Right-click "Run As" -> "Run Conciguration" -> Create JUnit test from left icon "JUnit" anyway;
回答4:
Make sure to have a valid default constructor for your test class.
回答5:
In my case, the problem was different. I was converting a TestNG test to JUnit. The @Test annotation was satisfied by the TestNG import, but that was the wrong annotation. I removed the TestNG import and added the JUnit import for @Test, and the right-click menu option to run as a JUnit test appeared.
回答6:
for junit5 jdk8 is needed in build path and project facets.
回答7:
I ran into these symptoms when importing an existing project into a Kepler-based Eclipse IDE for Java Developers version.
Importing the project into a Luna-based Eclipse IDE for Java EE Developers correctly set set it to a Java project (project icon now includes that little J) and now allows running JUnit tests.
回答8:
In my case the Java Build path (.classpath
file) was corrupt. In particular it had a merge conflict which was unresolved. Therefore, the JUnit 4 library was missing.
回答9:
I used to have a similar problem and it turned out that was because I forgot "extends Specification" after the "ClassToBeTestedSpec" in the declaration.
回答10:
Make sure that you include import org.junit.Test; and the tests have @Test before them, I have missed that before when coping some functions from other files.
回答11:
To solve the situation, a restart of eclipse wasn´t enough in my case, I had to remove and re-add the project to my workspace as well.
回答12:
In case you experience it in maven project add following into pom.xml and reload project using Alt+F5. Tests do not work because old JDK version.
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
回答13:
The eclipse shortcut to run Junit test is Alt+Shift+X, T
.
If its not working just press Alt+shift+X
a menu will popup just look for Junit.
来源:https://stackoverflow.com/questions/6365920/missing-run-as-junit-test