You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala 2.11.12+Play框架下Mockito依赖配置问题求助

Fixing Mockito Dependency Recognition Issues in Scala 2.11 + Play Framework

Hey there! Let's get to the bottom of why your Mockito imports aren't working and fix that compilation error once and for all.

The Root Cause

Your main problem is an extremely outdated Mockito dependency version (v0.30 is from 2009—way too old for Scala 2.11 and your Play setup!) and a missing compatibility layer between ScalaTest and Mockito (since you're using MockitoSugar).

Step 1: Replace the Broken Mockito Dependency

In your build.sbt, you've got this line that's causing the core issue:

libraryDependencies += "org.mockito" % "mockito-core" % "0.30" % "test"

Replace it with a stable, compatible Mockito Core version that works with Scala 2.11 (this was the latest stable release around 2019 when you posted):

libraryDependencies += "org.mockito" % "mockito-core" % "2.28.2" % Test

Since you're using org.scalatest.mockito.MockitoSugar in your tests, adding this dedicated compatibility dependency will eliminate any version conflicts between ScalaTest and Mockito:

libraryDependencies += "org.scalatestplus" %% "mockito-2-10" % "3.1.0.0" % Test

This version is built to work perfectly with your existing scalatestplus-play 3.1.2 and Scala 2.11 setup.

Step 3: Clean and Rebuild Your Project

Old compilation caches can cause weird issues even after fixing dependencies. Run these commands to refresh everything:

sbt clean test:compile test
  • clean wipes old cached files
  • test:compile ensures test dependencies are properly resolved
  • test runs your test suite to confirm the fix

Step 4: Tweak Your Test Code (Optional but Best Practice)

Quick note: Using thenReturn(null) isn't idiomatic Scala. If your getEmployeestructure method returns an Option (which it probably should), replace that line with:

when(mockedEmployeeDAO.getEmployeestructure(6)).thenReturn(None)

This aligns with Scala's null-safe practices.

Final Test Dependencies Section for build.sbt

Here's how your test-related dependencies should look after all changes:

libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % Test
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
libraryDependencies += "org.mockito" % "mockito-core" % "2.28.2" % Test
libraryDependencies += "org.scalatestplus" %% "mockito-2-10" % "3.1.0.0" % Test

内容的提问来源于stack exchange,提问作者Indu Chabada

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:32:24