Eclipse Oxygen.3中JUnit 5测试运行器提示“未找到测试”求助
Hey there, let's get to the bottom of that frustrating error you're seeing! The core issue here is that your test class uses JUnit 4 annotations and APIs, but Eclipse is trying to run it with the JUnit 5 test runner—they don't play well together out of the box. Here's how to fix it step by step:
1. Verify Your JUnit Dependencies
First, make sure your project has the correct JUnit 4 libraries attached:
- Right-click your project →
Build Path→Configure Build Path - Go to the
Librariestab and check if you have a JUnit 4 library (likeJUnit 4.x) listed. If not, add it by clickingAdd Library→JUnit→ SelectJUnit 4and finish. - If you're using Maven, double-check your
pom.xmlto ensure you're pulling in JUnit 4, not JUnit 5:<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>
2. Switch the Test Runner in Eclipse
This is the most likely fix for your error:
- Right-click your
MyMathTestclass →Run As→Run Configurations... - In the left pane, select the JUnit configuration for your test (or create a new one if needed)
- In the right pane, look for the
Test runnerdropdown menu—select "JUnit 4" instead of "JUnit 5" - Click
ApplythenRun—your tests should now be detected!
3. Tweak Your Test Class (Optional but Recommended)
Your current test class uses default package visibility (no public modifier). While JUnit 4 technically allows this, Eclipse can sometimes have trouble detecting non-public test classes. Try updating your class definition to:
public class MyMathTest { // rest of your code stays the same }
Also, double-check that you're importing the JUnit 4 @Test annotation:
// Correct JUnit 4 import import org.junit.Test; // NOT the JUnit 5 import // import org.junit.jupiter.api.Test;
Quick Look at Your Test Code
For reference, here's your code with the minor visibility tweak noted:
import static org.junit.Assert.assertEquals; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class MyMathTest { // Added public modifier MyMath myMath = new MyMath(); @Before public void before() { System.out.println("Before"); } @After public void after() { System.out.println("After"); } @Test public void testSum_with3numbers() { System.out.println("Test1"); // Don't forget to add your assertion logic here! // Example: assertEquals(6, myMath.sum(1, 2, 3)); } }
Give these steps a try—switching the test runner to JUnit 4 should resolve the "no tests found" error immediately.
内容的提问来源于stack exchange,提问作者Leem

