无法在scala playframework上运行单元测试

时间:2015-05-11 12:32:09

标签: scala unit-testing routing playframework-2.0 scalatest

我是playframework的新手。我正在尝试进行单元测试。这是我的OneTimeTestSuite文件。这是代码。

package com.sentrana.mmcore.test

import org.scalatest.{ DoNotDiscover, Suites }
import org.scalatestplus.play.OneAppPerSuite
import play.api.test.FakeApplication
import com.sentrana.mmcore.controllers.MetaDataSpec
import com.sentrana.Global

class OneTimeTestSuite extends Suites(
  new MetaDataSpec
) with OneAppPerSuite {
  implicit override lazy val app: FakeApplication =
    FakeApplication(withGlobal = Some(Global), additionalConfiguration = Map(
      "ConfigSubFolderLocation" -> "/conf/"
    ))
}

以下是MeteDataSpec文件中的代码。

package com.sentrana.mmcore.controllers

import play.api.Play
import play.api.Play.current
import org.joda.time.DateTime
import org.scalatestplus.play.{ PlaySpec, ConfiguredApp }
import play.api.libs.json.{ JsValue, Json }
import play.api.mvc.Cookie
import play.api.test.FakeRequest
import play.api.test.Helpers._
import org.json4s.native.Serialization._
import play.api.test._

class MetaDataSpec extends PlaySpec with ConfiguredApp {
  "GetCustomers" should {
    "get all the customers" in {
      val response = route(FakeRequest(GET, "/api/shop/v0.1/metadata/customers")).get
    }
  }
}

我正在使用scalatestplus

这是我正在运行测试的命令。

test-only com.sentrana.mmcore.test.OneTimeTestSuite

这是我得到的错误

java.lang.NoSuchMethodError: play.core.Router$HandlerDef.<init>(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Lscala/collection/Seq;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V

这是我的 build.sbt 文件。

name := "MMCorePlatform"

version := "0.1"

scalaVersion := "2.11.4"

resolvers += "softprops-maven" at "http://dl.bintray.com/content/softprops/maven"

libraryDependencies ++= Seq(
  "org.scalatestplus" %% "play" % "1.2.0" % "test",
  "mysql" % "mysql-connector-java" % "5.1.28",
  "postgresql" % "postgresql" % "9.1-901-1.jdbc4",
  "org.mongodb" %% "casbah" % "2.8.0",
  "org.json4s" %% "json4s-native" % "3.2.10",
  "org.json4s" % "json4s-mongo_2.10" % "3.2.10",
  "org.json4s" % "json4s-ext_2.10" % "3.2.10",
  "com.typesafe" %% "play-plugins-mailer" % "2.1-RC2",
  "com.typesafe" %% "scalalogging-slf4j" % "1.0.1",
  "org.squeryl" %% "squeryl" % "0.9.5-6",
  "com.amazonaws" % "aws-java-sdk" % "1.9.31",
  "me.lessis" %% "courier" % "0.1.3",
  "com.typesafe.akka" %% "akka-remote" % "2.2.3",   // very important : Need to synchronize akka-remote version with play version, otherwise wierd error might occur
  filters,
  jdbc,
  anorm,
  cache
)

dependencyOverrides += "com.google.guava" % "guava" % "15.0"

//Adds separate configuration file for test configuration
javaOptions in Test += "-Dconfig.file=conf/test.conf"

//Note, a separate integration test configuration that can be invoked using "int:test" has been created in project/Build.scala. 
//Adds separate configuration file for integration test configuration
javaOptions in IntTest += "-Dconfig.file=conf/int-test.conf"

//Adds "clRun" task to allow use of simple command line application to test application
val clRun = TaskKey[Unit]("clRun","Run task for command line application")

//Adds an additional resource directory used to read the file based meta-data repository 
unmanagedResourceDirectories in Compile += baseDirectory.value / "app/resources"

//Adds command line run task
fullRunTask(clRun, Compile, "com.sentrana.mmcore.commandline.TaskPlannerCommandLine", null)

play.Project.playScalaSettings

scalacOptions in (Compile,doc) := Seq("-groups", "-implicits")

//Sets ScalaTest tests to output junit xml format test output
testOptions in Test <+= (target in Test) map {
  t => Tests.Argument(TestFrameworks.ScalaTest, "-u", "%s" format (t / "test-reports"))
}

//Sets Specs2 tests to output junit xml format test output
testOptions in IntTest += Tests.Argument(TestFrameworks.Specs2, "junitxml")

//Publishing configurations
publishTo := {
  val nexus = "http://10.46.8.121:8081/nexus/"
  if (version.value.trim.endsWith("SNAPSHOT"))
    Some("snapshots" at nexus + "content/repositories/snapshots")
  else
    Some("releases"  at nexus + "content/repositories/releases")
}

publishMavenStyle := true

// Credentials for publishing to our internal repository
credentials += Credentials(Path.userHome / ".ivy2" / ".credentials") 

我希望我的问题清楚明了。如果有任何混淆,请询问。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的项目标记为scala 2.11,但您在2.10使用json4s。 Scala在主要版本之间没有二进制兼容性。

将您的依赖关系更改为

"org.json4s" %% "json4s-mongo" % "3.2.10",
"org.json4s" %% "json4s-ext" % "3.2.10",

%%会自动将scala版本后缀添加到工件名称,这是scala的惯例

相关问题