ScalaTest errors on assert and test in Intellij - scala

I am fairly new to Scala and Intellij and brand new to ScalaTest. I have set up Intellij 14 and Scala 2.11.4 with the latest Scala plugin and everything works fine. I then tried to introduce ScalaTest and I am very confused. I downloaded the latest version of ScalaTest (scalatest_2.11-2.2.1.jar) from scalatest.org. I went into my project structure and selected "Modules", clicked the green plus sign and added scalatest_2.11-2.2.1.jar. All went well and I can see the scalatest jar in External Libraries in my project. When I created my first test it imported org.scalatest.Funsuite and the class extended FunSuite and all was well. I then tried a very simple test that I copied from the Internet.
import org.scalatest.FunSuite
class MyTest extends FunSuite {
test("this is a test") {
assert(true)
}
}
I get 2 errors. It can not resolve the "assert" reference with that signature and for the "test" method it says unspecified value parameters Seq[Tag]: () => BoxedUnit.
I can see other method signatures for "assert" and "test" but I can not use the most basic ones.
Thanks for any help

While I didn't get exactly the same error messages, I had similar overall problems until I realized that you need two more JARs to use ScalaTest successfully: scala-reflect-2.11.4.jar and scala-xml-2.11-1.0.2.jar.
If you insist on doing this by hand you should also make sure that you have your tests in a directory that is marked as a "tests" directory -- so is highlighted in green: you can achieve this on the same "Modules" tab, but in the "Sources" sub-tab rather than "Dependencies". You also need to check that in the "Scope" column of the "Dependencies" sub-tab, the jars you added are marked as "Test" rather than "Compile".
If, on the other hand, you create an SBT project in IntelliJ rather than a regular Scala project, you could just add the following to your build.sbt:
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.1" % "test"
Re-import the build.sbt when prompted, then you can start writing and running tests: all the dependencies and test directories are set up correctly and it all "just works". I just created an empty project this way and ran your example. It took about a minute from beginning to end.
Obviously you're facing a trade-off between managing JARs by hand and learning yet another tool, but at this point learning the very basics of SBT may be worth your while.

Related

Scala "not found: object com" - should i really add entry in build.sbt if there are no other dependencies?

I have created basic Scala Play application with https://www.playframework.com/getting-started play-scala-seed. This project compiles and runs with sbt run. But I have another Scala project that compiles and runs and which I have submitted to my local Ivy repository with command sbt publishLocal. This other project was saved at C:\Users\tomr\.ivy2\local\com.agiintelligence\scala-isabelle_2.13\master-SNAPSHOT as a result of this command.
Then I imported (exactly so - imported, no just opened) my Play project in IntelliJ and I used Project - Open Module Settings - Project Settings - Libraries to add com.agiintelligence jar from my ivy2 location. After such operations IntelliJ editor recognizes com.agiintelligence classes. That is fine.
But when I am trying to run my Play application with sbt run, I experience the error message not found: object com that is exactly when compiling import com.agiintelligence line in my Scala controller file of Play application.
Of course - such error has been reported and resolved with, e.g. object play not found in scala application
But that solution suggests to append build.sbt file. My build.sbt file is pretty bare:
name := """agiintelligence"""
organization := "com.agiintelligence"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.5"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.skaraintelligence.controllers._"
// Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.skaraintelligence.binders._"
My Play application contains (as can bee seen from the IntelliJ project pane) some tens of 'external libraries' (it shows my com.agiintelligence jar as well), but why should I add my own ivy2 library in build.sbt file if no other libraries are listed here? What is different with my library? It is on my computer, in the repository as expected already?
Of course, I can try to add it build.sbt and issue sbt update and see what happens, but I can not understand this logic? Can someone explain it and provide some clue to intelligible solution of my error message?
My Play application contains (as can bee seen from the IntelliJ project pane) some tens of 'external libraries'
Those are probably just transitive dependencies of your Play dependency, that is why sbt downloaded all of them and put them in your classpath so you could use them without you needing to tell it about them; because the pom of Play already did.
It is not that the build tool or the IDE magically added all those dependencies for you because they read your mind and magically understood you wanted them. And that for some reason the magic stopped working for your own library.
Why it is not sufficient to list it Project-Setting--External Libraries in IntelliJ only?
That is sufficient for the IDE to work, but not for the build tool. The build tool is independent of the IDE; it doesn't know about it. sbt just knows about the dependencies you configured in your definition file.
Even more, you should always configure your dependencies on your build tool and then import that in the IDE; rather than the opposite. IDEs are graphical tools, so their state can not be committed, can not be shared, can not keep track of changes, can not be used in CI / CD environments; additionally, different teammates may want to use different IDEs.
I resolved the error message by adding line in build.sbt file
libraryDependencies += "de.unruh" %% "scala-isabelle" % "master-SNAPSHOT"
and by subsequent run of sbt update.
Error is solved, but the main question still stand - why I had to do this? Why there are tens of dependencies that are not listed in build.sbt and why should I list my dependency in build.sbt and why it is not sufficient to list it Project-Setting--External Libraries in IntelliJ only?
OK, comment by #Luis_Miguel_Mejía_Suárez gave the explanation, that comment is the actual and expected answer to my question.

ScalaTest v3: why require to implement convertToLegacyEqualizer

Using ScalaTest 3.0.0
Environment: Scala 2.11.8, sbt 0.13.5, IntelliJ 14.1.4
build.sbt has only
// NOTE: not using org.scalactic
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % "test"
The test below passed. However, IntelliJ marks a squiggly red line below MyMiniTest with the warning message:
Class 'MyMiniTest ' must either be declared abstract or implement
abstract member 'convertToLegacyEqualizer[T](left: T):
TripleEqualsSupport.this.LegacyEqualizer[T]' in
'org.scalactic.TripleEqualsSupport'
import org.scalatest.FeatureSpec
class MyMiniTest extends FeatureSpec {
scenario("A simple test") {
val a = 12
assert(a * 3 == 36)
}
}
What is the reason of this warning and what is the recommended solution to fix it?
I had the same problem on IntelliJ just follow this steps to invalidate cache/restart. This will solve the problem.
In my case it was a transitive dependency (don't know how a test library could appear as such) of a different version clashing with the dependency defined in my project. SBT knows how to deal with most of these cases, IntelliJ doesn't seem to know. Note that invalidating the cache and restarting IntelliJ wouldn't help in this case.
To be sure it's your case, check the following: File -> Project Structure -> [Project Settings - Libraries]. Look for org.scalatest:* and you will probably find two libraries, like this:
Then remove the unnecessary one by selecting it and pressing - at the top of the panel. That's it, IntelliJ will be happy now.
A cleaner solution would be to exclude the unnecessary library from your dependencies, e.g.:
ExclusionRule("org.scalatest", "scalatest_2.11-2.2.4")
IntelliJ will show the library among the project's dependencies, but will know that it should be ingored.
Please check all you dependencies and check if any of those dependencies is downloading org.scalatest.* . If the version of org.scalatest.* you have defined is different from the one getting downloaded due to other defined dependencies, this issue occurs.
I was using org.mockito%mockito-scala whose pom defined that scalatest 3.0.8 was provided. But the scalatest I had defined was 2.2.5. By changing the version of scalatest to 3.0.8, I was able to resolve this issue.
Hope this helps.

How to add com.typesafe.sbt.* dependencies to my project?

I was playing with one sbt web plugin and I wanted to reuse the code in my project. Unfortunately I wasn't even able to compile the original code in my project because of missing dependencies. These are the imports:
import com.typesafe.sbt.jse.SbtJsTask
import com.typesafe.sbt.web.{CompileProblems, LineBasedProblem}
import sbt.Keys._
import sbt._
import xsbti.Severity
None of these could be resolved. The build fails with messages like not found: object sbt. I checked the original project's build.sbt file but there was nothing relevant in libraryDependencies.
I'm using Intellij Idea and the strange thing is that when I expand External Libraries in the Project View I can find all the required stuff under SBT: sbt-and-plugins (for example object com.typesafe.sbt.web.CompileProblems is there and I can see its definition in the class file).
It seems to me that the stuff I need is a core part of sbt but somehow it won't load to the project. What am I doing wrong?
short answer: from here
EDIT (3): answer:
use a custom ivy resolver:
resolvers += Resolver.url("SBT Plugins", url("https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/"))(Resolver.ivyStylePatterns)
libraryDependencies += ("com.typesafe.sbt" % "sbt-js-engine" % "1.0.2")
.extra(
sbt.mavenint.PomExtraDependencyAttributes.SbtVersionKey -> sbtBinaryVersion.value,
sbt.mavenint.PomExtraDependencyAttributes.ScalaVersionKey -> scalaBinaryVersion.value)
.copy(crossVersion = CrossVersion.Disabled)
how to find plugin jars:
to figure out from where exactly sbt downloads the jars, I used this (somewhat awkward) process:
first, I wanted to see where sbt stores the file localy. so:
sbt "reload plugins" "show fullClasspath" | sed s/\),\ Attributed\(/\\n/g
and I searched the output (or you can use grep).
then, I deleted the file, and executed sbt again with: reload plugins, update & last update to see the full update log.
searching the log, I found a line saying where sbt got the plugin from.

Why don't specs2 + scalamock tests run in IntelliJ? Multiple suite traits detected

I'm trying to run Specs2 tests from IDEA from both Windows and Mac versions of Intellij IDEA 14. I generated the idea project files using both gen-idea and the built in SBT plugin and get the same results...
When I try to run them, I get
Error running Specs2 in 'scala': Test class not found
I've added packages and switched "search for tests" to in single module but still no avail. I also get the message below on the run config.
Multiple suite traits detected: List(ScTrait: SpecificationStructure, ScTrait: SpecificationStructure,)
The similar post How to run all Specs2 tests under IntelliJ IDEA? doesn't help and it all runs fine from SBT, BTW. I can't run individual tests via the short-cut either :'(
I'm suspecting it's a combination of scalamock and specs2, as if I remove the following from my build.sbt, I can run them again.
"org.scalamock" %% "scalamock-specs2-support" % "3.2" % "test"
Here's the run config.
You should check if you have multiple specs2 jars dependencies in your module. If you just leave one jar dependency the configuration error should disappear.
To help other googlers, it could also be that you have one package depending on specs2-core, and scalamock-specs2-support which depends on specs2. The solution is to add an explicit dependency on specs2, and both dependencies should be evicted to the one you import. I was just able to fix this with the following dependency: "org.specs2" %% "specs2" % "3.7" % "test"

SBT doesn't reconize junit testcase written in java file

I made a Scala/Java mixed project with SBT 0.11.2. My config for JUnit testing is
resolvers += "twitter.com" at "http://maven.twttr.com/"
seq(com.github.retronym.SbtOneJar.oneJarSettings: _*)
libraryDependencies += "com.novocode" % "junit-interface" % "0.10-M2" % "test"
When I write JUnit test cases in Scala with #Test, every goes well. But when I write a Java JUnit test case, then run test in sbt, the Java JUnit test cannot be reconized. Only test cases written in Scala are executed.
How can I make sbt recognize my Java and Scala test cases at the same time?
Probably late for the origional question, but..
I've just been looking at this. The JUnit tests in my project were not running for me until I ran sbt clean test. Now all working like a charm.
There was a bug in 0.11.x in detecting Java tests that was fixed in 0.12.0, although I didn't think it affected detecting annotated tests. You might try coming up with a minimal test case and checking with the latest sbt version (0.12.1). If the problem still exists, file a bug.
you should put your test classes into src/test/java and your class name should end with "Test" (for example myTest.java)