In the build.sbt of an Example Assignment in a Scala course, the test library used is junit 4.10. There is no mention of Scalatest.
libraryDependencies += "junit" % "junit" % "4.10" % Test
And yet in a test class scalatest could be referenced and the tests could be written using actual scalatest syntax:
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
#RunWith(classOf[JUnitRunner])
class ListsSuite extends FunSuite with Matchers {
...etc...
}
Question: I suppose the Scala compiler accesses scalatest via the junit library. If so, what is the reason to embed scalatest in junit?
I checked the assignment project from the referred course and it actually has quite complicated structure using deprecated way of defining a build in project/*.scala files, extending Build.
The answer to your question is simple though: scalatest dependency is defined in project/CommonBuild.scala and added to the build in project/StudentBuildLike.scala. So there is no magic, you're using normal scalatest and a custom test runner (see project/ScalaTestRunner.scala).
Related
I'm trying to write a test for my project in Scala 3. I added Scalatest library as :
libraryDependencies ++= Seq(
....
"org.scalatest" %% "scalatest" % "3.2.9" % Test
)
I know my structure is right:
But it gives me error:
value FunSuite is not a member of org.scalatest - did you mean scalatest.funsuite?
However, I used the same in another project and it works fine.
Thanks to #Johney
The correct usage is as below (at least in scala 3.0.2):
import org.scalatest.funsuite.*
class TestParser extends AnyFunSuite {
}
Of course the tutorials such as Getting started with FunSuite are based using import org.scalatest.FunSuite but the right examples is here which also referred as a first mention of FunSuite in Getting started with FunSuite .
I am trying to load an xml file using scala-xml_2.12-1.0.6.jar but it gives me NullPointerEexception while loading
Following is my line of code to load xml
import scala.xml.XML
val xml = XML.loadFile("sample.xml")
I have decompiled this jar and is method is present in that jar but for some reasons it is unable to find it in code.
I have Scala 2.13.1 on my system but for this project I am using scala 2.12.1 and it is mentioned in mu built.sbt
scalaVersion := "2.12.1"
I have following dependency in my built.sbt for this xml package
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.0.6"
If I copy and paste the same code to Scala interactive shell( scala 2.13.1) I get following error
import scala.xml.XML
^
error: object xml is not a member of package scala
did you mean Nil?
Can anyone please identify what am i doing wrong?
Thanks in advance.
I'm not sure how you are loading up the Scala REPL, but as mentioned in "How to use third party libraries with Scala REPL?", you should be launching the the REPL from SBT with sbt console. Your .sbt files will also need to be in scope.
The Scala REPL independently does not deal with .sbt files. They are 2 different tools.
Alternatively you could also install Ammonite-REPL which supports Magic Imports. You will be able to import Maven dependencies with import $ivy.
Scala 2.12 comes with scala-xml and you can remove that dependency as long as you run REPL with sbt console and not your native Scala REPL which is already # Scala 2.13. Otherwise you can also switch to Scala 2.13 for your SBT project.
I have Scala Play project which uses specs2 as the test framework. I'm having difficult time running the tests from IntelliJ. I get the following error:
InvocationTargetException for 'main' in org.specs2.NotifierRunner: null
Process finished with exit code 0
The tests run successfully from sbt. I have seen few other posts which says it could be due to ScalaTestPlus, but I don't use ScalaTest.
Here is what my dependencies look like
build.sbt
libraryDependencies ++= Seq(
ws,
guice,
specs2 % Test,
...
)
project/plugins.sbt
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.6")
...
The test looks like following:
#RunWith(classOf[JUnitRunner])
class MyAppSpec extends PlaySpecification with Mockito {
...
just leave a comment for someone still struggling, hope it help.
I faced the same error, it cause by i just use a different scala version with specs2. i specific scala binary version as 2.12 but import a specs2 compiled with 2.11.
Parallel collections in Scala 2.12 were importable out-of-the-box like so
import scala.collection.parallel.immutable.ParVector
val pv = new ParVector[Int]
however why in Scala 2.13 package scala.collection.parallel seems to be missing?
Parallel collections have been moved in Scala 2.13 to separate module scala/scala-parallel-collection
This Scala standard module contains the package
scala.collection.parallel, with all of the parallel collections that
used to be part of the Scala standard library.
For Scala 2.13, this module is a separate JAR that can be omitted from
projects that do not use parallel collections.
thus from 2.13 onwards we need the following dependency
libraryDependencies += "org.scala-lang.modules" %% "scala-parallel-collections" % "1.0.0"
and to enable .par extension method import
import scala.collection.parallel.CollectionConverters._
Corresponding scaladoc is also no longer available from 2.13 API docs but instead is published at javadoc.io/doc/org.scala-lang.modules/scala-parallel-collections_2.13.
My sbt project with scala-test is layout as standard:
sbt-example
build.sbt
src/main/scala/local/search/BinarySearch.scala
src/main/scala/local/util/Utility.scala
src/test/scala/local/search/SearchingSuite.scala
My build.sbt is very simple:
name := "helloworld"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.2.4"
)
My BinarySearch.scala looks like:
package local.search
object BinarySearch {
// def ...
}
My SearchingSuite.scala looks like:
package local.search
import org.scalatest.FunSuite
class SearchingSuite extends FunSuite{
// call functions from BinarySearch object
}
Run commands are:
sbt compile
sbt "test-only local.search.SearchingSuite"
Then sbt raises error:
not found: value BinarySearch
What's wrong with my code? And if I need to use some object from Utility.scala, how to import in SearchingSuite.scala?
Thanks, will vote up with any answers!
Let's say you are working on class A; and class A requires class B. As long as A and B belong to two different packages, you have to import class B.
In your case, SearchingSuite and BinarySearch belong to two different packages. In particular, the former belongs to the main source package, the latter belong to the test source package. Therefore, you must import BinarySearch in SearchingSuite. Otherwise, there will be an compilation error like what you have encountered.
To use some object from Utility, you
import local.util.Utility
if Utility is a class, or
import local.util.Utility._
if Utility is an object.
If you are using some IDE, like Intellij or Eclipse, right click on the error highlight, the IDE will pop up some suggestions.