一个简单的单元测试,当运行它时,得到错误"No tests found"

A simple unit test, when run it, get error "No tests found"

在我的 springboot 项目中,我不需要单元测试那些 mvc 的东西,而是一些纯 POJO classes。

我的 pojo class:

public class Calculator {

   public int add(int a, int b) {
       return a+b;
   }
}

我的测试class:

@ExtendWith(SpringExtension.class)
public class SimpleTest {
    @Test
    void testIt() {
      Calculator cal = new Calculator();
      int result = cal.add(1, 2);
      assertThat(result).isEqualTo(3);
    }
}

我正在使用 intellij IDE。在左边代码的 void testIt() 行旁边,有一个小的 运行 箭头,当我点击 运行 测试功能时,我得到失败:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [com.my.calculator.SimpleTest.testIt](filter.includeTestsMatching)

为什么我会收到此 No test found 错误?

顺便说一句,我的测试项目的依赖项:

testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.assertj:assertj-core'

你能检查一下你是否有以下部分:

test {
    useJUnitPlatform()
}

我相信 spring-boot-starter-test 导入了不同版本的 junit,即 jupiter 是 Junit 5 我相信 spring-boot-starter-test 使用 Junit 4 所以小心混合版本。我从下面的 spring-boot-starter-test 复制了 mvn 依赖项(它使用 junit 4)。

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>compile</scope>
</dependency>