Scala 2.11.12+Play框架下Mockito依赖配置问题求助
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
Step 2: Add ScalaTest-Mockito Compatibility (Recommended)
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
cleanwipes old cached filestest:compileensures test dependencies are properly resolvedtestruns 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

