maven - 在 Surefire Maven 插件中 fork : is it safe to use forking?

标签 maven unit-testing surefire

我们有several options在 Maven Surefire 插件中描述 fork 。其中一个选项,forkCount,解释如下:

Option to specify the number of VMs to fork in parallel in order to execute the tests. When terminated with "C", the number part is multiplied with the number of CPU cores. Floating point value are only accepted together with "C". If set to "0", no VM is forked and all tests are executed within the main process.

基于此,我可能会猜测,如果使用 fork 机制并重用 fork (reuseForks=true),那么同一个 JVM 将用于多个测试。这意味着,如果我已将某个类加载到内存中,则该类的静态成员可以在其他测试中重用并意外失败。

我的理解正确吗?

最佳答案

你是对的。使用静态元素的测试不是线程安全的,应该是 excluded from parallel execution :

If the Suite or Parameterized is annotated with @NotThreadSafe, the suite classes are executed in single thread. You can also annotate individual test class referenced by Suite, and the other unannotated test classes in the Suite can be subject to run in parallel.

Note: As designed by JUnit runners, the static methods annotated with @BeforeClass and @AfterClass are called in parent thread. Assign classes to the @NotThreadSafe Suite to prevent from this trouble.

关于maven - 在 Surefire Maven 插件中 fork : is it safe to use forking?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30874967/

相关文章:

javascript - 在Jasmine单元测试中,如何强制失败回调触发失败请求的结果?

java - Jacoco 的 Maven 多模块项目报告

windows - 如何在 Windows 控制台中设置 LOCALE

java - 加载位于 "src"maven 文件夹中的 fxml 文件

java - Spring Cloud配置云无需继承父级

使用我自己的模型作为参数执行单元测试时出现 java.lang.AssertionError

scala - 如何模拟 Akka Actor 来对类(class)进行单元测试?

testing - 如何使用 TestNG 生成 JUnit 样式的 XML 报告?

java - 为什么我在 ScalaIDE 中运行代码时会得到 `java.lang.NoClassDefFoundError: scala/Function1`?

rest - 无法访问 Swagger 2 UI,部署在外部 tomcat 上的 Spring Boot 应用程序