【问题标题】:Error on jUnit test with SBT使用 SBT 进行 jUnit 测试时出错
【发布时间】:2016-05-30 16:12:09
【问题描述】:

当我尝试使用 Spark、Scala 在我的 SBT 项目上创建 jUnit 测试时出现错误...

这是我主要的 buil.sbt:

name := "preowned-kittens"

val gitHeadCommitSha = taskKey[String]("Deteerminta el commit actual de git - SHA")

val makeVersionProperties = taskKey[Seq[File]]("hacer una archivo llamado version.properties.")

def PreownedKittenProject(name: String): Project = (
  Project(name, file(name)).
    settings(
      scalaVersion  := "2.11.7",
      version       := "1.0",
      organization  := "com.preowned-kittens",
      libraryDependencies ++= Seq(
        "org.specs2" %% "specs2-core" % "3.7" % "test",
        "com.novocode" % "junit-interface" % "0.11" % "test"
      )
    )
  )

gitHeadCommitSha in ThisBuild := Process("git rev-parse HEAD").lines.head

lazy val common = (
  PreownedKittenProject("common")
    settings(
    makeVersionProperties := {
      val propFile = new File((resourceManaged in Compile). value, "version.properties")
      val content = "version=%s" format (gitHeadCommitSha.value)
      IO.write(propFile, content)
      Seq(propFile)
    },
    resourceGenerators in Compile <+= makeVersionProperties
    )
  )

lazy val analytics = (
  PreownedKittenProject("analytics")
    dependsOn(common)
    settings()
  )

lazy val website = (
  PreownedKittenProject("website")
    dependsOn(common)
    settings()
  )

这些是我在 analitics/build.sbt 上使用的库:

libraryDependencies += "junit" % "junit" % "4.11" % "test"

libraryDependencies += "com.novocode" % "junit-interface" % "0.11"

我的 Java 测试是这样的:

LogicJavaTest.java

package org.preownedkittens;

import org.junit.*;
import scala.collection.immutable.*;

public class LogicJavaTest {
    @Test
    public void testKitten() {
        Kitten kitten = new Kitten(1, new HashSet());
        // in chapter 5 we have Assert.assertEquals(1, kitten.attributes().size());
        // but as part of the chapter, we correct it - this test should pass
        Assert.assertEquals(0, kitten.attributes().size());
    }
}

我在运行测试时遇到了这个问题:

[error] /Users/juliovg/Documents/trabajo/Desarrollos/Sbt/pruebas/sbtPractice/analytics/src/test/java/org/preownedkittens/LogicJavaTest.java:13:  scala.collection.immutable.HashSet cannot be converted to scala.collection.Seq<java.lang.String>
[error] new HashSet()
[info] Some messages have been simplified; recompile with -Xdiags:verbose to get full output
[error] (analytics/test:compileIncremental) javac returned nonzero exit code
[error] Total time: 8 s, completed 18-feb-2016 10:18:09

可能是我的问题。

谢谢

【问题讨论】:

  • Kitten 类的代码是什么?您将 HashSet 作为第二个参数传递,它需要一个 Seq...
  • 你能看到我的回答吗? - 谢谢
  • 您为什么要写一个只是对您问题的补充的答案?编辑问题
  • 关于如何将 JUnit 支持添加到 SBT 的相关问题 stackoverflow.com/questions/28174243/run-junit-tests-with-sbt

标签: java scala junit apache-spark sbt


【解决方案1】:

您正在尝试将 Java Set 传递给期望 Scala Seq 的构造函数,这是一个有序序列。您至少应该尝试传递一个 Java 列表,或更改 Kitten 类。

有关在 Java 和 Scala 集合之间转换的更多信息,请参阅 http://docs.scala-lang.org/overviews/collections/conversions-between-java-and-scala-collections.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-24
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2017-05-12
    相关资源
    最近更新 更多