首页 > 解决方案 > 为什么 jUnit 测试必须公开?

问题描述

当我尝试运行如下编写的测试时:

@Test
private void testGet() {
    assertTrue(true);
}

我收到以下异常 -java.lang.Exception: Method testGet() should be public

为什么私有方法不能作为单元测试执行?

标签: javaspringspring-bootjunit

解决方案


它不能是私有的,但不必是公开的。它可以是默认访问方法。它不能是私有方法,因为该方法必须由 junit 框架从类外部调用。


推荐阅读