单元测试控制器端点时Play Framework 2.6错误

问题描述 投票:1回答:1

我有以下测试:

import org.scalatestplus.play._
import play.api.test.Helpers._
import play.api.test.{FakeRequest, WsTestClient}
import com.github.andyglow.websocket._
import com.inland24.plantsim.controllers.ApplicationTestFactory
import org.scalatest.WordSpecLike
import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}    
class EventsWebSocketSpec extends PlaySpec with WordSpecLike
      with BaseOneServerPerSuite with ApplicationTestFactory
      with ScalaFutures with IntegrationPatience {

      private implicit val httpPort = new play.api.http.Port(9000)

      "Routes" should {

        "send 404 on a bad request" in  {
          route(app, FakeRequest(GET, "/boum")).map(status) mustBe Some(NOT_FOUND)
        }

        "send 200 on a good request" in  {
          route(app, FakeRequest(GET, "/")).map(status) mustBe Some(OK)
        }

      }
    }

我收到这个错误。似乎测试甚至没有开始:

Exception encountered when invoking run on a nested suite - org.slf4j.impl.SimpleLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext
java.lang.ClassCastException: org.slf4j.impl.SimpleLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext

它与我必须做的一些依赖性排除有什么关系吗?这是我的依赖项:

libraryDependencies ++= Seq(
  ws,
  // Our streaming library
  "io.monix" %% "monix" % "2.3.3",
  "io.monix" %% "monix-cats" % "2.3.3",

  // Dependencies needed for Slick
  "com.typesafe.slick" %% "slick" % "3.2.0",
  "com.typesafe.slick" %% "slick-hikaricp" % "3.2.0",

  // For application Metrics
  "io.dropwizard.metrics" % "metrics-core" % "4.0.0",
  "io.dropwizard.metrics" % "metrics-jvm" % "4.0.0",

  "com.typesafe.scala-logging" %% "scala-logging" % "3.8.0",
  "org.scala-lang.modules" % "scala-async_2.11" % "0.9.6",
  "com.typesafe" % "config" % "1.3.1",

  // For JSON parsing
  "com.typesafe.play" %% "play-json" % "2.6.0",
  "com.typesafe.play" %% "play-json-joda" % "2.6.0",

  // JDBC driver for MySQL & H2
  "mysql" % "mysql-connector-java" % "5.1.26",
  "com.h2database" % "h2" % "1.4.186",

  // Swagger UI API Docs
  "io.swagger" %% "swagger-play2" % "1.6.0",
  "org.webjars" %% "webjars-play" % "2.6.0-M1",
  "org.webjars" % "swagger-ui" % "2.2.0",

  // Test dependencies
  "com.typesafe.akka" %% "akka-testkit" % "2.5.2" % Test,
  "org.scalatest" %% "scalatest" % "3.0.1" % Test,
  "org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % Test,
  "com.github.andyglow" %% "websocket-scala-client" % "0.2.4" % Test
)

感谢任何帮助!

编辑:进一步探索,我认为罪魁祸首是这个slf4j绑定:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/joe/.ivy2/cache/org.slf4j/slf4j-simple/jars/slf4j-simple-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/joe/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]

如何摆脱这个?我知道在我的build.sbt中有一个依赖项,我必须排除slf4j,但那是哪一个?

logging playframework-2.6
1个回答
1
投票

通过反复试验,我发现这种方式将slf4j排除在我的两个依赖项之后,我能够解决这个问题!

  "org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % Test exclude ("org.slf4j", "slf4j-simple"),
  "com.github.andyglow" %% "websocket-scala-client" % "0.2.4" % Test exclude ("org.slf4j", "slf4j-simple")
© www.soinside.com 2019 - 2024. All rights reserved.