相关疑难解决方法(0)

SpringRunner 是否应该在 Spring Boot 中与 Junit 5 一起使用

我对 SpringRunner 类感到困惑。\n来自 Stackoverflow 和 google 确实理解了这一点SpringRunner并且SpringJunit4ClassRunner它们是一回事。

\n\n
@RunWith(SpringRunner.class)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我对这门课的理解是:

\n\n
    \n
  • 顾名思义,它是一个“运行程序”(它将运行 Junit 测试)
  • \n
  • 在使用注释执行单元测试时使用此类@RunWith
  • \n
  • 此注释有助于加载 spring 应用程序上下文并帮助 \xe2\x80\x98autowire\xe2\x80\x99 依赖项。
  • \n
  • 仅当我们需要 spring 上下文 bean 依赖项时才应使用此类。
  • \n
  • 所以这个类应该谨慎使用,否则我们将在测试过程中不必要地加载 spring 上下文。
  • \n
  • 此类帮助 Junit 和 Spring 协作。
  • \n
\n\n

有了 Junit5 和 spring boot,这个类不再需要了吗?

\n\n

如果是的话,我们应该在 Spring Boot 和 Junit5 环境中使用什么?

\n

java spring spring-boot

14
推荐指数
3
解决办法
1万
查看次数

@RunWith 未在 JUnit5 中编译

我的项目是使用 JUnit 5 设置的,我想使用不同的 Runner 进行测试,但我什至无法使用 @RunWith 注释,因为它无法编译。在本指南中,https://www.baeldung.com/junit-5-runwith,它说 RunWith 仍然应该在 JUnit 5 中工作,所以我很困惑为什么它甚至无法编译。
甚至https://junit.org/junit5/docs/current/user-guide/#writing-tests-dependency-injection也有一个使用 RunWith 的示例,如果你向下滚动一点:
Using @RunWith(JUnitPlatform.class) will output all reported entries to stdout.
那为什么我的代码不能编译?我是不是误会了什么?

运行 gradlew 构建:

TestGameController.java:12: error: cannot find symbol
@RunWith(CacioTestRunner.class)
 ^
  symbol: class RunWith
Run Code Online (Sandbox Code Playgroud)

java junit runner

4
推荐指数
1
解决办法
6803
查看次数

标签 统计

java ×2

junit ×1

runner ×1

spring ×1

spring-boot ×1