etorreborre / specs2

Software Specifications for Scala
http://specs2.org
Other
734 stars 214 forks source link

Exception running playframework 2.3.x app #356

Closed mdufresne closed 9 years ago

mdufresne commented 9 years ago

java.lang.ClassNotFoundException: ApplicationSpec$

STACKTRACE java.net.URLClassLoader$1.run(URLClassLoader.java:366) java.net.URLClassLoader$1.run(URLClassLoader.java:355) java.security.AccessController.doPrivileged(Native Method) java.net.URLClassLoader.findClass(URLClassLoader.java:354) java.lang.ClassLoader.loadClass(ClassLoader.java:425) sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) java.lang.ClassLoader.loadClass(ClassLoader.java:358) org.specs2.reflect.Classes$$anonfun$loadClassEither$1.apply(Classes.scala:134) org.specs2.reflect.Classes$$anonfun$loadClassEither$1.apply(Classes.scala:134) org.specs2.control.ActionT$$anonfun$safe$1.apply(ActionT.scala:89) org.specs2.control.ActionT$$anonfun$reader$1$$anonfun$apply$6.apply(ActionT.scala:80) org.specs2.control.Status$.safe(Status.scala:100) org.specs2.control.StatusT$$anonfun$safe$1.apply(StatusT.scala:62) org.specs2.control.StatusT$$anonfun$safe$1.apply(StatusT.scala:62) scalaz.syntax.ToApplicativeOps$$anon$1.self$lzycompute(ApplicativeSyntax.scala:29) scalaz.syntax.ToApplicativeOps$$anon$1.self(ApplicativeSyntax.scala:29) scalaz.syntax.ToApplicativeOps$ApplicativeIdV$$anonfun$point$1.apply(ApplicativeSyntax.scala:33) scalaz.WriterTApplicative$$anonfun$point$1.apply(WriterT.scala:268) scalaz.WriterTApplicative$$anonfun$point$1.apply(WriterT.scala:268) scalaz.effect.IO$$anonfun$apply$19$$anonfun$apply$20.apply(IO.scala:136) scalaz.effect.IO$$anonfun$apply$19$$anonfun$apply$20.apply(IO.scala:136) scalaz.FreeFunctions$$anonfun$return$1.apply(Free.scala:326) scalaz.FreeFunctions$$anonfun$return$1.apply(Free.scala:326) scalaz.std.FunctionInstances$$anon$1$$anonfun$map$1.apply(Function.scala:56) scalaz.Free$$anonfun$run$1.apply(Free.scala:172) scalaz.Free$$anonfun$run$1.apply(Free.scala:172) scalaz.Free.go2$1(Free.scala:119) scalaz.Free.go(Free.scala:122) scalaz.Free.run(Free.scala:172) scalaz.effect.IO$class.unsafePerformIO(IO.scala:22) scalaz.effect.IOFunctions$$anon$6.unsafePerformIO(IO.scala:227) org.specs2.runner.Runner$.execute(Runner.scala:29) org.specs2.runner.ClassRunner$class.run(ClassRunner.scala:43) org.specs2.runner.ClassRunner$.run(ClassRunner.scala:84) org.specs2.runner.ClassRunner$class.run(ClassRunner.scala:25) org.specs2.runner.ClassRunner$.run(ClassRunner.scala:84) org.specs2.runner.NotifierRunner.main(NotifierRunner.scala:12) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) org.jetbrains.plugins.scala.testingSupport.specs2.JavaSpecs2Runner.runWithNotifierRunner(JavaSpecs2Runner.java:101) org.jetbrains.plugins.scala.testingSupport.specs2.JavaSpecs2Runner.runSpecs2_new(JavaSpecs2Runner.java:188) org.jetbrains.plugins.scala.testingSupport.specs2.JavaSpecs2Runner.runSingleTest(JavaSpecs2Runner.java:201) org.jetbrains.plugins.scala.testingSupport.specs2.JavaSpecs2Runner.main(JavaSpecs2Runner.java:84) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

ApplicationSpec.scala

import org.specs2. import org.specs2.matcher. import org.specs2.specification.After import play.api.Play import play.api.libs.json.Json import play.api.test. import play.api.test.Helpers.

class ApplicationSpec extends Specification with JsonMatchers {

def is = args(sequential = true) ^ s2"""

This is a specification that tests the Application api endpoints

The api should send 404 on a bad request ${specTests.sendBadRequest} render index page ${specTests.getIndexPage} return newly created site with id ${specTests.createSite} update site document ${specTests.sendUpdatedSite} delete site document ${specTests.sendDeleteSite} """

object FakeApp extends FakeApplication

val specTests = AppSpecTest()

var testSiteId = 12

case class AppSpecTest() extends After { Play.start(FakeApp)

override def after = Play.stop()

def sendBadRequest = {
  route(FakeRequest(GET, "/boum")) must beNone
}

def getIndexPage = {
  val home = route(FakeRequest(GET, "/")).get

  status(home) must equalTo(OK)
  contentType(home) must beSome.which(_ == "text/html")
  contentAsString(home) must contain("Your new application is ready.")
}

def createSite = {
  val inputJson = Json.obj(
    "affiliate_id" -> 1,
    "status" -> 2,
    "title" -> "test site",
    "website" -> "www.testSite.com",
    "description" -> "described test",
    "unit_ids" -> List(1, 2, 3)
  )
  val fNewSite = route(FakeRequest(POST, "/create/site").withJsonBody(inputJson)).get

  status(fNewSite) must equalTo(CREATED)
  contentType(fNewSite) must beSome.which(_ == "application/json")
  testSiteId = TestUtils.validateInt(contentAsJson(fNewSite) \ "id")
  contentAsString(fNewSite) must /("id" -> TestUtils.doublePat) and
    /("affiliate_id" -> 1.0) and
    /("status" -> 2.0) and
    /("title" -> "test site") and
    /("website" -> "www.testSite.com") and
    /("description" -> "described test") and
    /("unit_ids").andHave(exactly("1.0","2.0","3.0"))
}

def readSite = {
  val fNewSite = route(FakeRequest(GET, s"/read/site/$testSiteId")).get

  status(fNewSite) must equalTo(OK)
  contentType(fNewSite) must beSome.which(_ == "application/json")
  println(contentAsString(fNewSite))
  contentAsString(fNewSite) must /("sites") /# 0 /("id" -> TestUtils.doublePat) and
    /("affiliate_id" -> 1.0) and
    /("status" -> 2.0) and
    /("title" -> "test site") and
    /("website" -> "www.testSite.com") and
    /("description" -> "described test") and
    /("unit_ids").andHave(exactly("1.0","2.0","3.0"))
}

def sendUpdatedSite = {
  val rn = TestUtils.getNextIdFromFile("sendUpdatedSite")
  val title = s"new site title$rn"
  val website = s"www$rn.testSite.com"
  val description = s"new $rn description"

  val inputJson = Json.obj(
    "id" -> testSiteId,
    "affiliate_id" -> 1,
    "status" -> 2,
    "title" -> title,
    "website" -> website,
    "description" -> description,
    "unit_ids" -> List(4, 5)
  )

  val fNewSite = route(FakeRequest(PUT, s"/update/site/$testSiteId").withJsonBody(inputJson)).get

  status(fNewSite) must equalTo(OK)
  contentType(fNewSite) must beSome.which(_ == "application/json")
  contentAsString(fNewSite) must /("id" -> testSiteId.toDouble) and
    /("affiliate_id" -> 1.0) and
    /("status" -> 2.0) and
    /("title" -> title) and
    /("website" -> website) and
    /("description" -> description) and
    /("unit_ids").andHave(exactly("4.0","5.0"))
}

def sendDeleteSite = {
  val fNewSite = route(FakeRequest(DELETE, s"/delete/site/$testSiteId")).get

  status(fNewSite) must equalTo(OK)
  contentType(fNewSite) must beSome.which(_ == "application/json")
}

}

}

build.sbt

import PlayKeys._

name := """PollwareAdmin"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.6"

resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"

//routesImport += "extensions.Binders._"

libraryDependencies ++= Seq( jdbc, anorm, cache, ws, "org.reactivemongo" %% "play2-reactivemongo" % "0.10.5.0.akka23", "org.specs2" %% "specs2-common" % "3.3.1" % "test", "org.specs2" %% "specs2-matcher" % "3.3.1" % "test", "org.specs2" %% "specs2-core" % "3.3.1" % "test", "org.specs2" %% "specs2-matcher-extra" % "3.3.1" % "test" )

scalacOptions in Test ++= Seq("-Yrangepos", "-feature", "-language:reflectiveCalls")

fork in run := true

etorreborre commented 9 years ago

It looks like you are running your app though IntelliJ. How does that work with sbt? Also can you make sure you have a recent IntelliJ Scala plugin?

mdufresne commented 9 years ago

After I removed Global.java (Play2 Global Object), the error went away. Thoughts?

On Sat, Apr 11, 2015 at 10:19 PM, Eric Torreborre notifications@github.com wrote:

It looks like you are running your app though IntelliJ. How does that work with sbt? Also can you make sure you have a recent IntelliJ Scala plugin?

— Reply to this email directly or view it on GitHub https://github.com/etorreborre/specs2/issues/356#issuecomment-91994002.

etorreborre commented 9 years ago

My only supposition is that code was effectively compiled to the right place (in particular the ApplicationSpec class) whereas it wasn't the case before. Well I'm glad it's working for you now :-).