Running ScalaTest in Play Framework 2.2.X - scala

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"

Related

Scalatest problem : value FunSuite is not a member of org.scalatest

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 .

Running specs2 test on IntelliJ throws InvocationTargetException

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.

How to find things and install them with Scala sbt?

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

Play+Scala suite test not found type PlaySpecification

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).

object db is not a member of package play

I'm trying to make my first tests with Scala and with Play framework.
I have installed play 2.2.0, which seems to be the last version, with the standalone package. After that, I've been able to create a new application, compile and run it.
I've tried to start to use Anorm package to access to the database, but I've found a blocking error which I can't find on the docs. I don't know if that means that is so obvious, but after adding:
package controllers
import play.api._
import play.api.mvc._
import play.db.anorm._ //(this is the new line)
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
}
It fails with:
object db is not a member of package play
I've seen this:
https://groups.google.com/forum/#!msg/play-framework/RUbmEVsw2rY/UOE5mNs1WjoJ
Where they talk about adding the dependency to jdbc, which seems to be already in my build.sbt.
libraryDependencies ++= Seq(
jdbc,
anorm,
cache
)
I've also found this thread here:
play.db package not found in play 2.1.3
But I can't find a build.scala file on my project. Not using any IDE by now, just play console (run & compile commands).
Thanks a lot!
In fact (as the error explains), there is no package play.db.anorm._ in version 2.2.0. Try use import anorm._ instead.
You need the following libraries
slick
play-jdbc
anorm
This is how my dependencies look like in build.sbt :
libraryDependencies ++= Seq(
"com.typesafe.slick" % "slick_2.10" % "2.1.0",
"org.postgresql" % "postgresql" % "9.4-1201-jdbc41",
"com.typesafe.play" % "play-jdbc_2.10" % "2.4.0-RC1",
cache,
anorm
)
Search for the latest version of library at Maven Central Repository