Relatively new to sbt and Mockito.
I want to use Mockito in tests, but I'm getting errors related to the Mockito imports when I compile the tests
Imports in test file:
import org.scalatest._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
sbt file:
name := "blah"
version := "0.1"
scalaVersion := "2.13.0"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
libraryDependencies += "org.mockito" % "mockito-core" % "1.8.5" % "test"
I get these error messages when the tests (fail to) compile:
object mockito is not a member of package org [error] import org.mockito.Mockito._
and also:
Symbol 'type org.mockito.MockSettings' is missing from the classpath.
[error] This symbol is required by 'value org.scalatest.mockito.MockitoSugar.mockSettings'.
I've had a play around with changing some of the versions of scalatest and mockito in the sbt file, but not really if that's getting at the root of the problem or not.
Thanks for any help!
You're using a very old version of Mockito, which is older than the one Scalates relies on, you probably need some 2.x.x version.
On the other hand, I'd recomend you to go fo mockito-scala rather than mockito-core and skip the Scalatest provided classes altogether as they are quite basic.
I suspect you have a caching problem. This happens especially with Intellij.
Here 2 ideas:
Reload the sbt project. See https://stackoverflow.com/a/20466144/2750966
Close the project / delete .idea an open the project newly with Intellij.
Let me know if it is not related with Intellij
Related
I am using intellij 2018 . I have dumped scalatest libraries through build.sbt and I could see libraries in external dependencies in project.
libraryDependencies += "org.scalatest" %% "scalatest" % "3.3.0-SNAP2" % Test
But when I import scalatest library in class it is showing not resolved.
how to make this work ?
import org.scalatest.flatspec.AnyflatSpec
I am new to Scala and struggling with sbt and installing things. For example:
I want to develop in Eclipse and hence use JUnit. According to:
http://www.scalatest.org/user_guide/using_junit_runner
"ScalaTest includes a JUnit Runner", that does not seem to be the case. At least not for me. (I get object is not a member of package org.scalatest when I try import org.scalatest.junit.JUnitRunner but import org.scalatest.flatspec.AnyFlatSpec works fine)
How do I install it? I looked at my sbt file, containing the lines:
libraryDependencies += "org.scalactic" %% "scalactic" % "3.1.1"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.1" % "test"
and thought OK so I need org.scalatest.junit and tried to add it like:
libraryDependencies += "org.scalatest" %% "junit" % "3.1.1"
which of course gave me three(!) screens of:
Note: Unresolved dependencies path:
[error] sbt.librarymanagement.ResolveException: Error downloading org.scalatest:junit_2.12:3.1.1
[error] Not found
(Why is it so repetitive? It's only one thing it hasn't found but it complains about it like four times!)
How do I figure out how to install things like this? Right now I am trying a combined approach of guesswork and random googling, sometimes it works and sometime (like now) it fails for me...
In ScalaTest 3.1.x to execute ScalaTest's suites using JUnit runner add the following dependencies to build.sbt
libraryDependencies ++= List(
"org.scalatest" %% "scalatest" % "3.1.1" % Test,
"org.scalatestplus" %% "scalatestplus-junit" % "1.0.0-M2" % Test
)
and annotate the suite like so
import org.junit.runner.RunWith
import org.scalatestplus.junit.JUnitRunner
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
#RunWith(classOf[JUnitRunner])
class HelloSpec extends AnyFlatSpec with Matchers {
"The Hello object" should "say hello" in {
Hello.greeting shouldEqual "hello"
}
}
There exists an open issue to update the documentation: Document how to use JUnitRunner in 3.x #1780
Here is my sbt file myproject/build.sbt
version := "1.0"
scalaVersion := "2.12.1"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.16",
"io.circe" %% "circe-core" % "0.6.1",
"io.circe" %% "circe-generic" % "0.6.1",
"io.circe" %% "circe-parser" % "0.6.1"
)
Here is my scala file myproject/src/test.scala
package mytest
import akka._
object test {
def main(args: Array[String]) {
print(2)
}
}
I verified that my external library contains, akka
but intellij keep saying that
Error:(7, 8) not found: object akka
import akka._
I am using intellij community edition 2016.3 with the latest scala plugin (which should include latest sbt)
Can someone give me a hint on how to resolve this?
To fix the problem, you have to place your Scala source file into src/main/scala directory. Otherwise IntelliJ/SBT can't recognize it as file related to the project, so it can't associate project dependencies with it.
By default Scala source files can be placed either in the root directory of your project, or in src/main/scala (for main sources, there is also src/test/scala for tests).
If you want to use some other directories to store your Scala source files, you can configure it this way in your build.sbt:
sourceDirectories in Compile += new File("src")
I had a similar problem and it was nothing to do with the directory structure in my case. IntelliJ asks you to refresh when you add a new dependency in build.sbt. I also manually refreshed it form the SBT Shell and still same error.
In the end I closed the project and re-opened and it was fixed.
I am doing the Scala course on coursera. I am trying to run the unit tests given in the exercise. However, I am getting the following issues:
Error:(3, 12) object scalatest is not a member of package org
import org.scalatest.FunSuite
^
Error:(6, 12) object junit is not a member of package org
import org.junit.runner.RunWith
^
These appear when I am importing packages into my project:
import org.scalatest.FunSuite
import org.junit.runner.RunWith
I googled this and found the following solution:
Add dependencies like this
libraryDependencies += "com.novocode" % "junit-interface" % "0.8" % "test->default"
I tried adding this code to build.sbt but the errors persist.
ScalaTest is not JUnit. Adding the junit-interface is needed if you want to execute JUnit tests directly with sbt. Obviously you are trying to run ScalaTest tests, though. The additional library dependency you need here is
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.5" % "test"
And then to include JUnit (instead of the junit-sbt-interface), probably this:
libraryDependencies += "junit" % "junit" % "4.12" % "test"
I have added the following in build.sbt:
libraryDependencies <<= scalaVersion { scala_version => Seq(
<other entries>
"org.scalacheck" %% "scalacheck" % "1.10.0" % "test",
<other entries>
)
}
Upon compile the project in sbt, the dependencies are resolved successfully as can be seen in the logs:
[info] Resolving org.scalacheck#scalacheck_2.9.1;1.10.0 ...
...
Done updating
However, I get the following error during compilation of one file.
object scalacheck is not a member of package org
import org.scalacheck.Gen
^
What can be the reason for this?
Is there a way to see the classpath that sbt is using during the compile task?
Sbt version: 0.11.2
OS: Windows 7
Scala version: 2.9.1
Note that the project builds fine in ScalaIDE. (I use sbteclipse to generate the eclipse .classpath file. The generated .classpath has proper scalacheck entry.)
The way you import the dependency makes Scalacheck only available for the command test:
"org.scalacheck" %% "scalacheck" % "1.10.0" % "test"
You should use:
"org.scalacheck" %% "scalacheck" % "1.10.0"
But why do you use Scalacheck somewhere else than in tests ? See this link for more explanations about testing in sbt.