import scalatest shows unresolved even after dumping maven dependency in intellij - scala

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

Related

How to find things and install them with Scala sbt?

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

object mockito is not a member of package org

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

Scala Intellij running test suite

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"

Can't use imports inside sbt build definition code

I tried importing JSON libraries in sbt, so that my custom sbt task can write a json file using a JSON api. However it seems like sbt can't import those libraries, but rather it can only import "standard" libraries like scala.io.Source, java.io.File, etc...
Both commented out lines below would each fail sbt:
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.3.7"
libraryDependencies += "io.argonaut" %% "argonaut" % "6.0.4"
compile in Compile <<= (compile in Compile) map { c =>
import scala.io.Source
//import play.api.libs.json.Json
//import argonaut._, Argonaut._
What might it be? Must I write a plugin to circumvent this?
$ about
[info] This is sbt 0.13.6
[info] The current project is built against Scala 2.11.6
[info] Available Plugins: ...
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4
Of course I can just string interpolate my json bare-handed but I wonder what might it be...
Thanks!
According to this snippet from the plugin documentation, you just need to include the dependency in plugins.sbt instead of your build definition (ie., no plugin is required).
// [within plugins.sbt]
// plain library (not an sbt plugin) for use in the build definition
libraryDependencies += "org.example" % "utilities" % "1.3"
So you should be able to just add these to plugins.sbt:
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.3.7"
libraryDependencies += "io.argonaut" %% "argonaut" % "6.0.4"

Why does sbt give "object scalacheck is not a member of package org" after successful scalacheck resolve?

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.