Running unit tests with ScalaTest

548 views
Skip to first unread message

Marco Massenzio

unread,
Sep 30, 2014, 6:18:33 PM9/30/14
to play-fr...@googlegroups.com
I'm relatively new to Scala and Play - however, I've been successfully running many ScalaTests against the 'core' code of my project without much difficulty.

I've now been struggling for almost two days in making work even the most basic examples described in the documentation:


I'm not sure what I'm doing wrong, but my suspicion is that either some sbt configuration imports the wrong version of ScalaTest + Play, or the code in the docs is plain wrong.

In my build.sbt I have:

scalaVersion := "2.10.4"

libraryDependencies ++= Seq(
  "org.scalatestplus" %% "play" % "1.1.0" % "test",
  "org.scalautils" %% "scalautils" % "2.1.4",
  jdbc,
  anorm,
  cache
)


I am importing the 2.3.0 sbt plugin for Play:

// in project/plugins.sbt

resolvers
+= "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.0")


And my unit test:
// test/ApplicationSpec.scala

import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.Play
import play.api.test.FakeApplication

class ApplicationSpec extends PlaySpec with OneAppPerSuite {

  // Override app if you need a FakeApplication with other than
  // default parameters.
  implicit override lazy val app: FakeApplication =
    FakeApplication(
      additionalConfiguration = Map("ehcacheplugin" -> "disabled")
    )

  "The OneAppPerSuite trait" must {
    "provide a FakeApplication" in {
      app.configuration.getString("ehcacheplugin") mustBe Some("disabled")
    }
    "start the FakeApplication" in {
      Play.maybeApplication mustBe Some(app)
    }
  }
}


In IntelliJ, ApplicationSpec is marked as a compilation error, as it is apparently missing a bunch of abstract methods (including triple equals, etc.) and if I run it, I get the following:

An exception or error caused a run to abort: ApplicationSpec.org$scalatest$words$MatcherWords$_setter_$typeCheck_$eq(Lorg/scalatest/words/TypeCheckWord;)V 
java.lang.AbstractMethodError: ApplicationSpec.org$scalatest$words$MatcherWords$_setter_$typeCheck_$eq(Lorg/scalatest/words/TypeCheckWord;)V
at org.scalatest.words.MatcherWords$class.$init$(MatcherWords.scala:244)
at org.scalatestplus.play.PlaySpec.<init>(PlaySpec.scala:34)
at ApplicationSpec.<init>(ApplicationSpec.scala:5)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:433)
at org.scalatest.tools.Runner$.genSuiteConfig(Runner.scala:2644)
at org.scalatest.tools.Runner$$anonfun$37.apply(Runner.scala:2461)
at org.scalatest.tools.Runner$$anonfun$37.apply(Runner.scala:2460)
....

If I run `activator test` in the project's directory, I get instead:

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)
        at sbt
.Defaults$$anonfun$detectTests$1.apply(Defaults.scala:555)

Does anyone know what am I doing wrong?
I've been doing endless searches in Google and StackOverflow, the documentation on this is incredibly thin on the ground and it's virtually impossible to figure out what goes on behind the scenes.

Is it possible that it's just not possible to use ScalaTest with Play 2.3.0 for testing, and we can only use spec2?

Thanks in advance!






Marcos Savoury

unread,
Jan 2, 2015, 9:01:51 AM1/2/15
to play-fr...@googlegroups.com
Hi Marco,

Were you able to resolve the issue that you described?  I am facing the same issue as well and I have been pulling my hair out trying to figure out what is going on.

Marcos Savoury

unread,
Jan 2, 2015, 9:12:15 AM1/2/15
to play-fr...@googlegroups.com
For anyone else who might find this post, I just figured out what the issue was. I was importing incompatible versions of the plugins.  Verify that the versions match as defined on the page: http://www.scalatest.org/plus/play 
Funny how a good night's sleep is the most effective problem-solving tool we have. 

Marco Massenzio

unread,
Jan 2, 2015, 12:49:19 PM1/2/15
to play-fr...@googlegroups.com
Glad you figured it out.
The full explanation of the issue was reported here:

in my own answer.

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/dlkwguKT3z0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages