You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse Oxygen.3中JUnit 5测试运行器提示“未找到测试”求助

Fixing "No tests found with test runner JUnit 5" in Eclipse Oxygen.3

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 Libraries tab and check if you have a JUnit 4 library (like JUnit 4.x) listed. If not, add it by clicking Add Library → JUnit → Select JUnit 4 and finish.
  • If you're using Maven, double-check your pom.xml to 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 MyMathTest class → 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 runner dropdown menu—select "JUnit 4" instead of "JUnit 5"
  • Click Apply then Run—your tests should now be detected!

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:03:42