是这样的,今天单元测试,碰到这个错。
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 4
和Junit 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,希望能帮到你。

未经允许不得转载:老郭种树 » JUnit Java.lang.Exception: No runnable methods
最新评论
我刚在无痕浏览器、Safari都试过,密码是对的。以后本帖关于密码的问题不再回复。
错了,好吗
真的错了,输了十次都是错的
2,3挂了,1,5实际上都是星火的资源,可以关注分享者的公众号和*里网盘订阅。
感谢老郭无私分享