如何在 sbt 中运行 java/scala 单元测试

Posted

技术标签:

【中文标题】如何在 sbt 中运行 java/scala 单元测试【英文标题】:how to run java/scala unit test in sbt 【发布时间】:2022-01-02 20:03:03 【问题描述】:

我是 scala 的新手, scala 项目包含 scala 和 java 单元测试用例。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import itest.Level2;
    @RunWith(Suite.class)
    @Suite.SuiteClasses(Level2.class) // Level2.class is contains @test method
    class  OrganizationRegexModelComponentTester  implements ITest
    
    

itest.jar 包含文件 Level2.class。 我加了

libraryDependencies += "junit" % "junit" % "4.11" % "test" // already added
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"

testOnly company.extraction.api.util.TestJunitTwo 
[info] ScalaTest
[info] Run completed in 313 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] No tests were executed.
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[success] Total time: 7 s, completed Nov 24, 2021 1:17:53 PM

当我运行时 - sbt test 它只运行 scala 测试而不是 java 测试。 如何同时运行 java 和 scala 测试来运行。

【问题讨论】:

可能与 ***.com/questions/28174243/run-junit-tests-with-sbt 相关。您的 sbt test 正在运行不基于 JUnit 的 ScalaTest。 【参考方案1】:

简答:尝试删除@RunWith(Suite.class)

【讨论】:

是的,删除了 @RunWith(Suite.class) 并添加了 import org.junit.experimental.categories.Categories @RunWith(classOf[Categories]) 工作正常

以上是关于如何在 sbt 中运行 java/scala 单元测试的主要内容,如果未能解决你的问题,请参考以下文章

在多模块 Java/scala 项目中有一个通用的 webapp?

RISC-V 酷刑测试的先决条件(java、scala、sbt 版本)是啥?

Windows上Intellij + Java + Scala + Sbt环境搭建

如何在 sbt 中运行和编译准确的一项测试?

如何构建运行测试套件的 Maven 工件? “sbt 测试”如何工作?

在 SBT 中使用“sbt testOnly”从 jar 中运行测试?