GuiceApplicationBuilder 和 PlayTest 测试出错,在运行路径上找不到播放/配置

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

我们使用 GuiceApplicationBuilder 通过 fakeApplication 设置 Playframework 测试

class BaseSpec extends AnyFunSpec/PlaySpec with GuiceOneAppPerSuite {
  override def fakeApplication(): Application = new GuiceApplicationBuilder()
    .disable[PlayModule]
    .disable[JobSchedulerModule]
    .build()
}

我们有(经过净化的)测试设置,例如

import BaseSpec

class SampleSpec extends BaseSpec {
   "test suite" should {
      "test case" in {
          .....
      }
   }
}

每当我们运行测试时,我们都会得到:

A needed class was not found. This could be due to an error in your runpath. Missing class: play/Configuration
java.lang.NoClassDefFoundError: play/Configuration
    at play.inject.BuiltInModule.bindings(BuiltInModule.java:22)
    at play.api.inject.guice.GuiceableModuleConversions.guice(GuiceInjectorBuilder.scala:365)
    at play.api.inject.guice.GuiceableModuleConversions.guice$(GuiceInjectorBuilder.scala:364)

版本: sbt:1.6.0-M1 JDK:11.0.24 sbt 播放:2.8.8 scala-sbt:2.12.15

scalaVersion := "2.12.15"
val akkaVersion = play.core.PlayVersion.akkaVersion

libraryDependencies ++= Seq(
  guice,
  ws,
  "org.skinny-framework"        %% "skinny-orm"                   % "3.1.0"           withSources(),
  "org.scalikejdbc"             %% "scalikejdbc-play-initializer" % "2.8.0-scalikejdbc-3.5"withSources(),
  "org.scalikejdbc"             %% "scalikejdbc"                  % "3.3.1"           withSources(),
  "com.typesafe.play"           %% "play-slick"                   % "5.0.2"           withSources(),
  "com.auth0"                    % "java-jwt"                     % "3.2.0", // Don't update it! will have jackson conflict
  "org.scalatestplus.play"      %% "scalatestplus-play"           % "5.1.0"           % Test,
  "org.scalatestplus"           %% "mockito-5-12"                 % "3.2.19.0"        % Test,
  "org.mockito"                  % "mockito-all"                  % "1.10.19"         % Test,
  "com.amazonaws"                % "aws-java-sdk"                 % "1.11.313",
  "org.flywaydb"                %% "flyway-play"                  % "7.14.0",
  "org.postgresql"               % "postgresql"                   % "42.2.2",
  "com.chuusai"                 %% "shapeless"                    % "2.3.3",
  "org.typelevel"               %% "cats-core"                    % "1.1.0",
  "org.lyranthe.prometheus"     %% "client"                       % "0.9.0-M5",
  "io.split.client"              % "java-client"                  % "2.3.1",
  "com.segment.analytics.java"   % "analytics"                    % "2.1.0",
  "it.innove"                    % "play2-pdf"                    % "1.9.1",
  "org.webjars"                  % "swagger-ui"                   % "2.2.0",
  "com.fasterxml.jackson.module" %% "jackson-module-scala"        % "2.11.4",
  "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310"   % "2.11.4",
  "com.github.jasminb"             % "jsonapi-converter"         % "0.11",
  "de.leanovate.play-mockws"    %% "play-mockws"                  % "2.8.1"            % Test,
  "org.scalatest"                %% "scalatest"                   % "3.2.19"           % Test,
  "javax.xml.bind"               % "jaxb-api"                     % "2.3.1",
  "org.scala-lang"               % "scala-compiler"               % scalaVersion.value,
  "org.scala-lang"               % "scala-reflect"                % scalaVersion.value,
  "org.scala-lang"               % "scala-library"                % scalaVersion.value,
  "com.typesafe.play"            %% "play"                        % "2.8.8",
  "com.typesafe.play"            %% "play-guice"                  % "2.8.8",
  "com.typesafe.play"            %% "play-test"                   % "2.8.8"           % Test,
  "com.typesafe.akka"            %% "akka-testkit"                % akkaVersion       % Test,
  "com.typesafe.akka"            %% "akka-http"                   % "10.2.10"         % Test
)

dependencyOverrides ++= Seq(
  "com.typesafe.akka"           %% "akka-actor"                % akkaVersion      withSources(),
  "com.typesafe.akka"           %% "akka-stream"               % akkaVersion      withSources(),
  "com.google.guava"             % "guava"                     % "22.0",
  "org.slf4j"                    % "slf4j-api"                 % "2.0.16",
  "org.bitbucket.b_c"            % "jose4j"                    % "0.7.0",
)

不需要假应用程序的测试不会抛出此异常。 此异常是我们尝试从旧版本升级应用程序的结果。 我们尝试更改 GuiceOneAppPerSuite 的逻辑,但它不起作用。 我们尝试升级PlayFramework的版本,仍然不行。 我们可以确认播放/配置是我们运行路径的一部分。

scala playframework sbt guice
1个回答
0
投票

该错误让我认为您正在使用 Java 导入而不是 Scala 导入。 Play 的某些类在不同的包下可用于 Java 和 Scala。

Configuration
类也是如此。您想要
play.api.xxx
课程(注意
api
)。

检查您的

import
的陈述。

© www.soinside.com 2019 - 2024. All rights reserved.