JUnit Java.lang.Exception: No runnable methods

是这样的,今天单元测试,碰到这个错。

JUnit 测试提示 Java.lang.Exception: No runnable methods
org.junit.runners.model.InvalidTestClassError: Invalid test class 
Runner org.junit.internal.runners.ErrorReportingRunner () does not support filtering and will therefore be run completely.

导致原因很值得警惕,所以分享在这。

就我上次分享的 SpringBoot 单元测试,@RunWith 找不到

新版的 SpringBoot 都是使用 Junit 5,如果要单元测试,直接使用@SpringBootTest就行,不用@RunWiht

当然,如果测试要启动服务,运行 Spring 容器,要配置如下。

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

但都不用配置@RunWiht

很尴尬,我这个单元测试类是从老项目复制过来的,就带了如上注解,甚至配置的值是Junit 4

@RunWith(SpringJUnit4ClassRunner.class)

然后导包那就将Junit 4Junit 5混用了。

import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;

运行就报如上错误。

解决很简单,要么全部使用 Junit 4,我直接列出完整代码吧。

import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MainTests {
    @Test
    public void test() {
     
    }
}

要么使用Junit 5

import org.junit.jupiter.api.Test;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MainTests {
    @Test
    public void test() {
   
    }
}

OK,希望能帮到你。

本文由老郭种树原创,转载请注明:https://guozh.net/junit-java-lang-exception-no-runnable-methods/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注