使用GuiceApplicationBuilder与PlayTest测试报错:play/Configuration未找到
Play Framework 测试中
play/Configuration 类找不到问题 测试代码结构
基础测试类
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-play: 2.8.8
- scala: 2.12.15
sbt 依赖配置
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", )
已知信息
- 不需要启动 Fake Application 的测试不会触发该异常
- 问题出现在应用版本升级之后
- 已尝试修改
GuiceOneAppPerSuite逻辑、升级 Play Framework 版本,均无效 - 已确认
play/Configuration类存在于运行路径中
排查与解决建议
- 排查依赖冲突:执行
sbt dependencyTree查看依赖树,确认是否有其他依赖引入了不同版本的 Play 核心包,尤其是包含play/Configuration的模块。 - 调整
GuiceApplicationBuilder配置:禁用PlayModule会导致核心配置组件未加载,尝试移除该禁用项,或手动绑定配置组件:override def fakeApplication(): Application = new GuiceApplicationBuilder() .disable[JobSchedulerModule] .bindings(bind[Configuration].toInstance(Configuration.load())) .build() - 清理 sbt 缓存:执行
sbt clean、sbt reload、sbt test:compile,清理旧编译文件,避免类加载异常。 - 修正依赖覆盖:Play 2.8.x 默认依赖 slf4j 1.7.x,
dependencyOverrides中强行指定的 2.0.16 版本可能不兼容,建议移除该项。 - 检查测试类继承结构:确保
BaseSpec未混入其他干扰应用初始化的 trait,继承关系正确。
内容的提问来源于stack exchange,提问作者P-Man-Coding
相关产品推荐
相关产品推荐

