Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

unit testing - Run JUnit tests with SBT

I have a 0.13.7 SBT project, with several sub-projects.

One of them is called webapp, and it has many JUnit tests in webapp/src/test/java.

When running:

sbt webapp/test

only the ScalaTest tests are run, but no JUnit tests.

Snippet of my build.sbt file:

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
            )
        ): _*
    )

Example JUnit test:

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));
    }
}

UPDATE (some more research):

> 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)

So JUnit support is known by SBT but not loaded.

Debug output:

[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,false,org.scalatest.tools.Framework$$anon$1@97f54b))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon$2@6a589982))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon$2@1b95d5e6))
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon$1@5c997dac))
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon$1@406c43ef))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon$2@282ddefc))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon$2@4400c80))

Working with:

  • SBT 0.13.9, and
  • JUnit 4.x.

Related information:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally, I've discovered, that I have to add the following settings to the subproject:

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

It is important to declare the junit-interface dependency in the subproject, and in addition, to set to false the crossPaths setting.

The clue has been given by this issue.

If the main project doesn't have JUnit tests, then the needed test settings, don't need to be provided.

In addition, for knowing the failing method and the cause, we need this setting:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...