JUnit5 tests work fine with maven but not when run through Eclipse, "No tests found with test runner 'JUnit 5'."

SOL*_*OLO 4 eclipse maven junit5

I'm getting a pop-up window with title "Could not run test" and message "No tests found with test runner 'JUnit 5'." when I try to run JUnit 5 tests with Eclipse via Run As > JUnit Test.

I have two test-related files. One is a test suite:

...

import org.junit.jupiter.api.BeforeAll;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SelectPackages("com.foo.stufftotest")

public class TestSuite {
    @BeforeAll
    public static void setup() {
        ...
Run Code Online (Sandbox Code Playgroud)

The other contains the "actual" tests:

package com.foo.stufftotest;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import com.foo.TestSuite;
import com.foo.business.mechanics.LogicStuff1;

public class BusinessTest {
    @Test
    public void testLogic1() {
        ...
    }

    @Test
    public void testLogic2() {
        ...
    }

    ...
Run Code Online (Sandbox Code Playgroud)

All the testLogicN() methods depend on setup stuff done in TestSuite.setup(). If setup() doesn't run, there are a lot of null values, and it's no surprise that things fail. When I try to run JUnit from the project's context menu, all the tests are triggered and they all fail; the suite doesn't seem to be recognized. When I try to run JUnit specifically from TestSuite.java's context menu, I end up with the error I mentioned at the top of the question.

However, when I run maven test on the project, the suite is triggered properly, and all the tests pass. Therefore, the code itself doesn't seem to be the problem.

I don't remember having this issue with JUnit 4, although I never used JUnit 4 with this particular project.

Am I using Eclipse wrong, or am I using JUnit5 wrong? What's the fix here?

小智 9

尽管我的单元测试已使用正确注释,但我在Eclipse Oxygen 4.7.1中遇到了相同的问题org.junit.jupiter

我使用Maven > Update Project和选择了,Update project configuration from pom.xml但是什么也没做,尽管我认为它会接受我在pom中具有JUnit5依赖关系的事实。

我的解决方案:

  1. 打开Java Build Path,选择Libraries选项卡,然后选择Add Library
  2. 选择JUnit
  3. 选择JUnit5JUnit库版本。

添加后,我便能够从Eclipse手动执行测试。但是我仍然不确定为什么没有自动添加。


Sam*_*nen 3

我收到一个弹出窗口,标题为“无法运行测试”,消息为“未使用测试运行程序‘JUnit 5’找到测试”。当我尝试通过 Eclipse 使用 Eclipse 运行 JUnit 5 测试时Run As > JUnit Test

那是因为TestSuite它实际上不是 JUnit 5 测试类。由于它带有@RunWith(来自 JUnit 4)注释,因此它是一个 JUnit 4 测试类。为了避免在 Eclipse 中弹出窗口,只需单击“运行配置”并选择 JUnit 4 而不是 JUnit 5 来​​运行测试类。

您遇到的另一个问题是@BeforeAll来自 JUnit Jupiter 的注释。因此,用 注解的类根本不支持它@RunWith(JUnitPlatform.class)(除非该类也恰好包含@TestJUnit Jupiter 的方法)。因此,您必须找到一种替代方法来执行“设置”代码。