Scalatest and scalamock - dependency trouble involving SuiteMixin - scala

I'm trying to setup my project to use scalatest and scalamock.
I'm using scala version 2.10.0.
However, I cant seem to get the dependencies right.
I've started with this code:
class ControllerTest extends org.scalatest.FunSuite
with org.scalamock.scalatest.MockFactory {}
I've tried two combinations of versions:
1)
org.scalatest : scalatest_2.10 : 1.9.1
org.scalamock: scalamock-scalatest-support_2.10 : 3.0.1
This is what I get:
scala: bad symbolic reference.
A signature in MockFactory.class refers to type SuiteMixin in package org.scalatest which is not available.
It may be completely missing from the current classpath, or the version on the classpath might be incompatible with the version used when compiling MockFactory.class.
Note: in the scalamock documentation, the artifact id is specified without the trailing _2.10, but maven couldn't find any artifact named like this. Also, I couldn't find on their site what scalatest version should be used with scalamock.
2)
org.scalatest : scalatest_2.10 : 1.9.1
org.scalamock: scalamock-scalatest-support_2.10.0-RC5: 3.0-M8
The compiler says:
scala: overriding method nestedSuites in trait SuiteMixin of type => scala.collection.immutable.IndexedSeq[org.scalatest.Suite];
method nestedSuites in trait Suite of type => List[org.scalatest.Suite] has incompatible type
class ControllerTest extends FunSuite with MockFactory {
and
scala: class ControllerTest needs to be abstract, since:
it has 5 unimplemented members.
/** As seen from class ControllerTest, the missing signatures are as follows.
* For convenience, these are usable as stub implementations.
*/
def rerunner: Option[String] = ???
def run(testName: Option[String],args: org.scalatest.Args): org.scalatest.Status = ???
protected def runNestedSuites(args: org.scalatest.Args): org.scalatest.Status = ???
protected def runTest(testName: String,args: org.scalatest.Args): org.scalatest.Status = ???
protected def runTests(testName: Option[String],args: org.scalatest.Args): org.scalatest.Status = ???
So, what's up with this SuiteMixin trait?
If I use scalatest-support_2.10.0-RC5:3.0-M8, it appears to exist in scalatest lib.
If I use scalatest-support_2.10:3.0.1, it seems to have gone from said scalatest lib.
What kind of sorcery is this? And, more importantly, what version combination should I use to make it work?
Thanks!

If you have this dependency
"org.scalamock" %% "scalamock-scalatest-support" % "3.0.1" % "test"
It will automatically download the correct version of scalatest. In this case it's
org.scalatest#scalatest_2.10;2.0.M5b!scalatest_2.10.jar
In most cases where one library depends upon another you just add only that one library as dependency. Sbt-like tools will get the other dependencies.

EECOLOR's answer is correct. To elaborate, the cause of the problem was that the ScalaMock version you chose was compiled against a later version of ScalaTest (2.0.M5b) than the one you were explicitly trying to use (1.9.1).

Related

value should is not a member of String - Scala2.10

I want to cross compile my project across different Scala versions (2.10, 2.11, 2.12). I get this error message while compiling specs for 2.10 only. 2.11 and 2.12 work fine:
value should is not a member of String
Spec class looks as follows:
class ClassNameSpec extends WordSpec with Matchers {
// ...
}
I have also tried changing WordSpec to FlatSpec and still getting same error.
Using gradle - build.gradle has following related dependencies:
classpath 'gradle.plugin.com.github.maiflai:gradle-scalatest:0.19'
compile "org.scala-lang:scala-library:${scalaVersion}"
compile "org.scalactic:scalactic${scalaVersionSuffix}:3.0.5"
testRuntime 'org.pegdown:pegdown:1.4.2'
How can I make WordSpec or FlatSpec with Matchers work for Scala2.10? If not, what's the best work around?

nimbus-jose-jwt Plugin Type Not Found Error

I am trying to compile code sample from this book (Play Framework Cookbook 2nd Edition) for JWT with nimbus-jose-jwt.
However it said Plugin trait type not found during compilation. I checked API documentation, Plugin is available at play.api package.
import play.api.{Logger, Play, Application, Plugin}
class JWTPlugin #Inject() (app: Application) extends Plugin {...}
I tried DI version with and without
routesGenerator := InjectedRoutesGenerator at build.sbt with same error.
nimbus-jose-jwt version: 3.8.2. Tried with latest 4.22, with same error.
scala: 2.11.8
play: 2.5.4
Appreciate any pointer. Thanks.
play.api.Plugin has been removed in Play 2.5.x. You can turn the plugin into a Module instead as described here: https://www.playframework.com/documentation/2.5.x/PluginsToModules
But the easiest solution is to turn the JWTPlugin class into a plain Scala object (e.g. JWTUtil), remove the onStart and onStop methods, remove the "private val jwt" declarations/assigments from all files in the example and call the sign() and verify() methods on the JWTUtil object instead.
Maybe you need some more minor modifications that I have forgotten, but anyway, the example works for me when the JWT implementation doesn't inherit from play.api.Plugin.

Unable to use ScalaCheck with Scala IDE for Eclipse

I'm trying to follow the instructions in this guide http://lamp.epfl.ch/files/content/sites/lamp/files/teaching/progfun/ScalacheckTutorial.html
in order to use ScalaCheck in Scala IDE.
However, when I create the first file StringSpecification.scala, with this code:
package pp.scalacheck
import org.scalacheck._
import Prop.forAll
object StringSpecification extends Properties("String") {
property("startsWith") = forAll { (x: String, y: String) =>
(x + y).startsWith(x)
}
}
I get the following error in Scala IDE:
bad symbolic reference to scala.util.parsing encountered in class file
'CmdLineParser.class'. Cannot access term parsing in package
scala.util. The current classpath may be missing a definition for
scala.util.parsing, or CmdLineParser.class may have been compiled
against a version that's incompatible with the one found on the
current classpath. (NOTE: It looks like the scala-parser-combinators
module is missing; try adding a dependency on "org.scala-lang.modules"
: "scala-parser-combinators". See
http://docs.scala-lang.org/overviews/core/scala-2.11.html for more
information.)
Error in Scala compiler: bad symbolic reference to
scala.util.parsing.combinator encountered in class file
'CmdLineParser.class'. Cannot access term combinator in value
scala.util.parsing. The current classpath may be missing a definition
for scala.util.parsing.combinator, or CmdLineParser.class may have
been compiled against a version that's incompatible with the one found
on the current classpath. (NOTE: It looks like the
scala-parser-combinators module is missing; try adding a dependency on
"org.scala-lang.modules" : "scala-parser-combinators". See
http://docs.scala-lang.org/overviews/core/scala-2.11.html for more
information.)
SBT builder crashed while compiling. The error message is 'bad
symbolic reference to scala.util.parsing.combinator encountered in
class file 'CmdLineParser.class'. Cannot access term combinator in
value scala.util.parsing. The current classpath may be missing a
definition for scala.util.parsing.combinator, or CmdLineParser.class
may have been compiled against a version that's incompatible with the
one found on the current classpath. (NOTE: It looks like the
scala-parser-combinators module is missing; try adding a dependency on
"org.scala-lang.modules" : "scala-parser-combinators". See
http://docs.scala-lang.org/overviews/core/scala-2.11.html for more
information.)'. Check Error Log for details.
I'm using the latest stable version of Scla IDE (for Eclipse) (3.0.4), ScalaCheck (2.11-1.11.6) and Scala (2.11.2), and I'm running on Windows.
What could be causing this error?
When I added this jar to the project I was able to build the example files:
http://search.maven.org/#artifactdetails%7Corg.scala-lang.modules%7Cscala-parser-combinators_2.11%7C1.0.2%7Cbundle

SBT: Plugin dependencies and project classpath

How does one add a external dependency to a SBT plugin and make it available on both the project and plugin classpath ?:
Specifically I have a simple plugin that should run our TestNG test suites and do some post processing. Here is a simplified version:
import sbt._
import java.util.ArrayList
import Keys._
import org.testng._
object RunTestSuitesPlugin extends Plugin {
lazy val runTestSuites = TaskKey[Unit]("run-test-suites", "runs TestNG test suites")
lazy val testSuites = SettingKey[Seq[String]]("test-suites", "list of test suites to run")
class JavaListWrapper[T](val seq: Seq[T]) {
def toJavaList = seq.foldLeft(new java.util.ArrayList[T](seq.size)) { (al, e) => al.add(e); al }
}
implicit def listToJavaList[T](l: Seq[T]) = new JavaListWrapper(l)
def runTestSuitesTask = runTestSuites <<= (target, streams, testSuites) map {
(targetDirectory, taskStream, suites) =>
import taskStream.log
log.info("running test suites: " + suites)
runSuites(suites)
}
private def runSuites(testSuites: Seq[String]) = {
var tester = new TestNG
tester.setTestSuites(testSuites.toJavaList)
tester.run()
}
def testSuiteSettings = {
inConfig(Compile)(Seq(
runTestSuitesTask,
testSuites := Seq("testsuites/mysuite.xml"),
libraryDependencies += "org.testng" % "testng" % "5.14"))
}
}
The problem is that when I add this plugin to a project and run it with run-test-suites then it fails with java.lang.NoClassDefFoundError: org/testng/TestNG even though show full-classpath shows that testng.jar is on the classpath.
So somehow the classpath used when executing the plugin differs from the one in my project, so how do I make a plugin dependency appear in both places ?
I'll try an answer, but I'm not very familiar with the inner details of sbt.
Normally, the path for the build system (as opposed to your program) is under project, as explained here. That would typically be in a project/plugins.sbt. Sounds right, as there is no reason that the application you develop should be concerned by what libraries your build system uses, nor the other way round.
When your plugin run the application code, that may not be so simple and there could well be classpath/classloader issues. I'm not sure that it will work. Normally, your plugin should implement a testing Framework rather than define its own task. Documentation of testing for sbt is limited.
A testing framework should implement org.scalatools.testing.Framework, in test-interface. Your build will take it into account after you add
testFrameworks += new TestFramework("full.class.name")
When you run the normal test command, it let every framework recognize the test classes it deals with (two criteria available: extending some base class or having some annotation) and run them. The framework run in the build, it is given a class loader to access the application code.
You may have a look at the framework implementation for junit (shipped with sbt). Also there is a TestNG implementation. I don't know it, according to its doc, it is a little bit unorthodox, hopefully it will work for you.
The error was fixed by adding TestNG directly to unmanagedJars in Compile in the project that uses the plugin.
I have not found any resources explaining the structure of the SBT class path during plugin execution so any attempt at explaining why this step is necessary will be greatly appreciated.

Error in scala + dispatch

I am trying to GET a response from an API, using scala and dispatch. However, I get this error, after building. I googled for a solution, and tried cleaning, and resarting eclipse, but the error wont go away. What seems to be the problem? I use eclipse Helios (ie 3.6) and Scala v2.8.1, with Scala IDE v1.0.0.201104170033, installed from the Eclipse market.
dispatch{dispatch.type}.Http{object dispatch.Http} of type object dispatch.Http does not take parameters
This is my code.
class getList {
def main(args: Array[String]){
Http("http://foo.com/" >>> System.out)
}
}
What am I doing wrong?
What libraries have you downloaded? Are you sure the dependencies are set correctly? I tried with dispatch_http, version 8.0 for Scala 2.8.1, and it worked.
What imports are you using? I used these imports to make it work:
import dispatch.Http
import dispatch.HandlerVerbs._
Finally... class getList??? I assume this is a result of cut&pasting from actual code, but you should strive to produce a compilable example of your problem. Scala doesn't run programs from class, only from object, and it follows Java style of having classes start with an uppercase letter.
Here's the minimal code I used with SBT to get a working example.
Initializing:
~/test$ sbt
Project does not exist, create new project? (y/N/s) y
Name: test
Organization: test
Version [1.0]:
Scala version [2.7.7]: 2.8.1
sbt version [0.7.4]:
~/test$ cat project/build/TestProject.scala
import sbt._
class TestProject(info: ProjectInfo) extends DefaultProject(info) {
val dvers = "0.8.0"
val http = "net.databinder" %% "dispatch-http" % dvers
}
~/test$ cat src/main/scala/GetList.scala
import dispatch.Http
import dispatch.HandlerVerbs._
object GetList {
def main(args: Array[String]){
Http("http://foo.com/" >>> System.out)
}
}
~/test# sbt update run