加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

单元测试 – 使用SBT运行JUnit测试

发布时间:2020-12-15 00:58:03 所属栏目:Java 来源:网络整理
导读:我有一个0.13.7 SBT项目,有几个子项目. 其中一个被称为webapp,它在webapp / src / test / java中有许多JUnit测试. 运行时: sbt webapp/test 只有ScalaTest测试运行,但没有JUnit测试. 我的build.sbt文件的代码段: libraryDependencies ++= Seq( "com.novoco
我有一个0.13.7 SBT项目,有几个子项目.

其中一个被称为webapp,它在webapp / src / test / java中有许多JUnit测试.

运行时:

sbt webapp/test

只有ScalaTest测试运行,但没有JUnit测试.

我的build.sbt文件的代码段:

libraryDependencies ++= Seq(
    "com.novocode" % "junit-interface" % "0.11" % Test
)

lazy val webapp = project
    settings(
        Seq(
            projectDependencies ++= Seq(
                ....
                "org.scalatest" %% "scalatest" % "2.2.2" % Test,"junit" % "junit" % "4.11" % Test,"com.novocode" % "junit-interface" % "0.11" % Test
            )
        ): _*
    )

示例JUnit测试:

import org.junit.Test;

public class CodificadorBase64Test {
    @Test
    public void testPlain() {
        byte b[] = {64,127,72,36,100,1,5,9,123};
        assertEquals("QH9IJGQBBQl7",CodificadorBase64.encode(b));
    }
}

更新(一些更多的研究):

> webapp/testFrameworks
[info] List(TestFramework(WrappedArray(org.scalacheck.ScalaCheckFramework)),TestFramework(WrappedArray(org.specs2.runner.Specs2Framework,org.specs2.runner.SpecsFramework)),TestFramework(WrappedArray(org.specs.runner.SpecsFramework)),TestFramework(WrappedArray(org.scalatest.tools.Framework,org.scalatest.tools.ScalaTestFramework)),TestFramework(WrappedArray(com.novocode.junit.JUnitFramework))

show webapp/loadedTestFrameworks
[info] Map(TestFramework(WrappedArray(
  org.scalatest.tools.Framework,org.scalatest.tools.ScalaTestFramework)
) -> org.scalatest.tools.Framework@65767aeb)

所以JUnit支持是SBT已知的,但没有加载.

调试输出:

[debug] Framework implementation 'org.scalacheck.ScalaCheckFramework' not present.
[debug] Framework implementation 'org.scalacheck.ScalaCheckFramework' not present.
[debug] Framework implementation 'org.scalacheck.ScalaCheckFramework' not present.
[debug] Framework implementation 'org.scalacheck.ScalaCheckFramework' not present.
[debug] Framework implementation 'org.specs2.runner.Specs2Framework' not present.
[debug] Framework implementation 'org.specs2.runner.Specs2Framework' not present.
[debug] Framework implementation 'org.specs2.runner.Specs2Framework' not present.
[debug] Framework implementation 'org.specs2.runner.Specs2Framework' not present.
[debug] Framework implementation 'org.specs2.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs2.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs2.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs2.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs.runner.SpecsFramework' not present.
[debug] Framework implementation 'com.novocode.junit.JUnitFramework' not present.
[debug] Framework implementation 'com.novocode.junit.JUnitFramework' not present.
[debug] Framework implementation 'com.novocode.junit.JUnitFramework' not present.
[debug] Framework implementation 'com.novocode.junit.JUnitFramework' not present.
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon$1@3ad42aff))
[debug] Subclass fingerprints: List((org.scalatest.Suite,org.scalatest.tools.Framework$$anon$1@97f54b))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,org.scalatest.tools.Framework$$anon$2@6a589982))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,org.scalatest.tools.Framework$$anon$2@1b95d5e6))
[debug] Subclass fingerprints: List((org.scalatest.Suite,org.scalatest.tools.Framework$$anon$1@5c997dac))
[debug] Subclass fingerprints: List((org.scalatest.Suite,org.scalatest.tools.Framework$$anon$1@406c43ef))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,org.scalatest.tools.Framework$$anon$2@282ddefc))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,org.scalatest.tools.Framework$$anon$2@4400c80))

使用方法:

> SBT 0.13.9,和
> JUnit 4.x.

相关信息:

> Why don’t junit tests get executed with “sbt test”?
> SBT documentation

解决方法

最后,我发现,我必须将以下设置添加到子项目中:
lazy val webapp = project
    settings(
        Seq(
            projectDependencies ++= Seq(
                ....
                "org.scalatest" %% "scalatest" % "2.2.2" % Test,crossPaths := false,"com.novocode" % "junit-interface" % "0.11" % Test
            )
        ): _*
    )

重要的是在子项目中声明junit-interface依赖关系,此外,将crossPaths设置为false.

线索已由this issue提供.

如果主项目没有进行JUnit测试,那么不需要提供所需的测试设置.

另外,为了知道失败的方法和原因,我们需要这个设置:

testOptions in Test := Seq(Tests.Argument(TestFrameworks.JUnit,"-a"))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读