java - 如何使用 JUnit 和 Ant 运行测试?在同一个 JVM 中还是不同的 JVM 中?

标签 java ant junit jvm

我在使用 JUnit 和 Ant 时面临一个小问题。我不知道应该如何进行测试。我应该使用 forked="yes" 选项为每个测试创建一个新的虚拟机,还是应该使用相同的虚拟机?

哪个是更好的方法,或者我如何知道何时使用相同的 JVM?

谢谢

最佳答案

要在 Ant 中运行 JUnit 测试,请使用 junit任务。可以选择在单独的 JVM 中运行测试(fork 属性)。

一般来说,最好在单独的 VM 中运行测试,以将它们与 Ant JVM 及其类路径隔离。这还允许为测试配置 JVM 的属性,例如最大内存量(任务的许多属性仅在启用 fork 时应用)。

但是,由于创建新 JVM 的开销,这会使测试花费更长的时间。这还取决于 forkmode 属性。如果设置为 perTest,每个测试将在其自己的虚拟机中运行。

fork 模式

Controls how many Java Virtual Machines get created if you want to fork some tests. Possible values are "perTest" (the default), "perBatch" and "once". "once" creates only a single Java VM for all tests while "perTest" creates a new VM for each TestCase class. "perBatch" creates a VM for each nested and one collecting all nested s.

关于java - 如何使用 JUnit 和 Ant 运行测试?在同一个 JVM 中还是不同的 JVM 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26873889/

相关文章:

java - 从 Java 7 调用 SQLCMD

java - 在java中查找匹配后的字符串

java - 无法为 JSP : cannot be resolved to a type 编译类

java - Apache CXF org.apache.cxf.transport.http.HTTPException : HTTP response '404: Not Found' when communicating to end point URL

java - 如何在 Ant 中使用带有 loadproperties 的 Base64 编码?

java - 有没有办法强制测试方法?

java - 如何使用这个 Color 的构造函数? java

java - 生成 Checkstyle 报告(带有扩展检查)

java - 如何使用 Java 测试使用 sleep() 的方法?

java - 从 eclipse 执行时 Spring 4.1.7 + Junit 4.12 不工作