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
Related
I want to start writing simple tests in Scala using ScalaTest.
But for some reason, I can access org.scalatest but not org.scalatest.FunSuite
This is what my build.sbt looks like:
name := "Algorithms"
version := "0.1"
scalaVersion := "2.13.3"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.2.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.0" % "test"
I don't understand if it can access scalatest then why are FunSuite,FlatSpec and other styles missing?
Output of running test on sbt shell
[error] <Project Path>\Algorithms\src\test\scala\Course1\Week1\MaxPairProductTest.scala:3:48: type FunSuite is not a member of package org.scalatest
[error] class MaxPairProductTest extends org.scalatest.FunSuite {
[error] ^
ScalaTest 3.2.0 has completed modularisation of the monolith from prior versions
The main change in ScalaTest 3.2.0 is carrying out the modularization
that we prepared for in 3.0.8 and 3.1.0. As a result, many deprecated
names have been removed, because the deprecations would cross module
boundaries.
This means that whilst in 3.1.0 the following definition
import org.scalatest.FunSuite
class ExampleSuite310 extends FunSuite {}
would just raise deprecation notice
The org.scalatest.FunSuite trait has been moved and renamed. Please use org.scalatest.funsuite.AnyFunSuite instead. This can be rewritten automatically with autofix: https://github.com/scalatest/autofix/tree/master/3.1.x", "3.1.0"
in 3.2.0 it has been removed entirely. Hence from 3.2.0 onwards you should define like so
import org.scalatest.funsuite.AnyFunSuite
class ExampleSuite320 extends AnyFunSuite {}
See deprecations expirations for full list of new names.
Note we can still import a single artifact which will pull transitively all the sub-artifacts
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.0" % "test"
however now we also have the options of depenending on just the particular sub-artifact
libraryDependencies += "org.scalatest" %% "scalatest-funsuite" % "3.2.0" % "test"
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
It is my first web app using Play Framework and Scala. I am following this tutorial > https://semaphoreci.com/community/tutorials/how-to-add-integration-tests-to-a-play-framework-application-using-scala and after the app working well, my tests cannot work. When I type sbt test the compiler says cannot find PlaySpecification.
[error] /home/felipe/Documentos/AMC/amc-project/play-scala-library/test/controllers/HomeControllerIntegrationSpec.scala:6: not found: type PlaySpecification
[error] class HomeControllerIntegrationSpec extends PlaySpecification {
These are my dependencies >
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
evolutions,
"com.typesafe.play" %% "anorm" % "2.4.0",
"commons-codec" % "commons-codec" % "1.6",
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
)
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
There are two frameworks for testing in play available, specs2 and scalatest. You only need to use one though, not both. In your case things got a bit mixed up:
PlaySpecification is a trait for testing with specs2. However your dependencies contain only scalatestplus (and hence scalatest transitively) which means you're using scalatest to run your tests. In this case you want to use PlaySpec instead, that's the base class for scalatest suites.
Alternatively you can of course switch to specs2 instead, including it's dependency makes PlaySpecification available. Just add specs2 % Test to your dependencies in this case (and best remove scalatestplus).
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.