Running specs2 test on IntelliJ throws InvocationTargetException - scala

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.

Related

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

Changes to build.sbt to enable ScalaTest tests to run with Scala Play

I am following the docs here, and this does not seem to work at all; but I'm not even sure what am I doing wrong here.
For a start, just adding the given snippet in a project/Build.scala file causes a compile error; so that's a non-start.
Wrapping it inside an object which extends Build (as in the SBT example) does not cause a compile error, but the tests are not run.
Eventually, I've added the following to my build.sbt
libraryDependencies ++= Seq(
"org.scalatestplus" %% "play" % "1.0.0" % "test",
...
this is marginally better, but in IntelliJ, my ApplicationSpec has all sorts of compilation error:
import org.scalatestplus.play._
import scala.collection.mutable.Stack
class ApplicationSpec extends PlaySpec {
"A Stack" must {
"pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() mustBe 2
stack.pop() mustBe 1
}
"throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a [NoSuchElementException] must be thrownBy {
emptyStack.pop()
}
}
}
}
must, mustBe and a are not recognized.
Finally, if I try to run activator test I get:
sentinel/test:definedTests
java.lang.NoSuchMethodError: org.specs2.runner.Fingerprints$.fp1()Lorg/specs2/runner/SpecificationFingerprint;
at org.specs2.runner.Specs2Framework.fingerprints(SbtRunner.scala:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sbt.TestFramework$.getFingerprints(TestFramework.scala:113)
at sbt.Tests$$anonfun$discover$1.apply(Tests.scala:242)
at sbt.Tests$$anonfun$discover$1.apply(Tests.scala:242)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251)
at scala.collection.AbstractTraversable.flatMap(Traversable.scala:105)
at sbt.Tests$.discover(Tests.scala:242)
at sbt.Defaults$$anonfun$detectTests$1.apply(Defaults.scala:556)
...
[error] (sentinel/test:definedTests) java.lang.reflect.InvocationTargetException
Essentially, an epic fail - it would be great if the docs were a bit more specific in what is required and what is expected for all the machinery to work: as things stand, it's kinda difficult to untangle the mess.
I've been googling and looking here on stack overflow for hours today, looked at the ScalaTestPlus docs (well, the whole two paragraphs of it...) and tried many variations, all to no avail.
The full project's code is on github.
Worth noting that if I give in to the "power-of-bad-documentation" and I use Specs2, then tests run correctly (at least from inside Intellij, and as far as the 'hello world' example goes).
I would still much prefer to using ScalaTest (can't really see why I need to learn TWO testing frameworks, really) - so, any help would be appreciated.
In the end, it turned out it was indeed a library version mismatch, but one I hadn't anticipated.
It revolves around the fact that I have a sub-project in my sbt build, which works just fine, but was importing ScalaTest 2.2.1, while I was using 2.1.7 to make it work with ScalaTest+ 1.1.0 - however, using ScalaTest 2.1.7 (which, indeed works with ST+ 1.1.0) only for the 'top-level' project was causing the weirdness around the abstract class, etc.
The Versions, Versions, Versions page has now been updated by Bill, and the fix has been to use ScalaTest 2.2.1, ScalaTestPlus 1.2.0 and Play 2.3.0 - which all play nice together.
Updated - the above still caused tests to fail via sbt/activator test, I could only run the tests via IntelliJ: the fix has been to update the build.sbt to use Scala 2.11
In the end, to make this work, this is what I have in my build.sbt:
name := "sentinel"
version := "0.2-SNAPSHOT"
organization := "AlertAvert.com"
scalacOptions ++= Seq("-deprecation", "-feature", "-language:postfixOps")
// The REST project depends on Core Sentinel classes
lazy val sentinel_core = project
lazy val sentinel = (project in file("."))
.enablePlugins(PlayScala)
.aggregate(sentinel_core)
.dependsOn(sentinel_core)
scalaVersion := "2.11.1"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.2.1" % "test",
"org.scalatestplus" %% "play" % "1.2.0" % "test",
cache
)
(I'm reproducing it in its entirety, so that if someone else has the same problem, they'll see it - it infuriates me when people only paste a fragment and some key essentials are omitted :)
Major credits to Bill Venners for the help and guidance.
Appears you have library version mismatch somewhere.
I have been using Scalatest with Play and haven't had any issues.
I am using Play 2.3.4, ScalaTest 2.2.1.
activator "test-only com.abc.tests.controllers.ApplicationSpec"
[info] ApplicationSpec:
[info] A Stack
[info] - must pop values in last-in-first-out order
[info] - must throw NoSuchElementException if an empty stack is popped
[info] ScalaTest
[info] Run completed in 1 second, 119 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.

Running ScalaTest in Play Framework 2.2.X

I'm trying to run some functional ScalaTest in Play 2.2 but can't seem to be able to import de #Test annotation needed to run the tests. I've tried looking for solutions but they seem to be from different versions since none work with my 2.2 version.
Can anyone guide me on running a ScalaTest some on play?
The super basic test class I tried to run is:
class Test extends AssertionsForJUnit {
#Test def test() {
}
}
While the error I get is:
type mismatch; found : server.Test required:
scala.annotation.Annotation
Can you paste an example of your test class? Also you need to include ScalaTest in the build file:
libraryDependencies += "org.scalatest" %% "scalatest" % "2.1.0" % "test"

Scala Playframework 2.2 - HtmlUnit missing

I have a project using Playframework 2.2
I eclipsified it and in my code I write something like this:
import com.gargoylesoftware.htmlunit.WebClient
val client = new WebClient
Eclipse says everything's fine, but when I play run my application I get a following message:
[error] C:\workspace\kwiket\kwiketscala\app\service\parsers\HTMLParser.scala:11: object gargoylesoftware is not a member of package com
[error] import com.gargoylesoftware.htmlunit.WebClient
How could I fix this? What dependencies should I add to my project? Thanks
In the test configuration is HTMLUnit available, but you need it in the compile configuration.
Just update your libraryDependencies in build.sbt:
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
"net.sourceforge.htmlunit" % "htmlunit" % "2.13" //found on http://search.maven.org/#artifactdetails%7Cnet.sourceforge.htmlunit%7Chtmlunit%7C2.13%7Cjar section SBT
)
Restart SBT or execute reload.

How to run scala specs 2 in eclipse

How to run scala specs 2 in eclipse (scala-ide)?
My steps were:
sbt eclipse - generate eclipse project from existing source
eclise: import project - open project in eclipse ide
Put break-point in my SPECS 2 test (class MyTest extends Specification { ... )
Try to run test ...? (usually I do it in IntelliJ IDEA.. but today going to check how eclipse works with debug in scala..)
At the moment, you can use a little hack. If you annotate your Specs2 class as follows:
#RunWith(classOf[JUnitRunner])
With older versions of specs2, this should be:
#RunWith(classOf[JUnitSuiteRunner])
The Scala IDE can run it with the JUnit runner simply through the popup menu (see the details here).
I'm working on a Specs2 plug-in for the Scala IDE (well, unfortunately I haven't really found time for it for a while, but it's still on my list).
It has a working version for the previous version of the Scala IDE, it is a bit difficult to set it up but works. See the details here: http://rlegendi.github.io/specs2-runner/
I had to make sure the build.sbt file included the following information:
scalacOptions in Test ++= Seq("-Yrangepos")
"org.specs2" %% "specs2-core" % "3.6.5" % "test",
"org.specs2" %% "specs2-junit" % "3.6.5" % "test",